可能有点抽象,先看applicationContext.xml中的内容:<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- 连接数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/ZuyingSoft"></property>
  <property name="username" value="root"></property>
  <property name="password" value="***"></property>
</bean>

    <!--在使用Struts2时,由于在struts.xml中有配置userAction,所以在UserAction中,可以通过setDataSource取到dataSource-->
    <bean id = "userAction" class = "com.action.UserAction"> 
  <property name="dataSource">
<ref local="dataSource"/>
  </property>      
    </bean>
    
    <!--由于Test类没有配置struts.xml,虽然我也把dataSource注入到了Test类中去了,setDataSource也有,但依然取不到dataSource的值,怎么办?-->
    <bean id = "test" class = "com.test.Test"> 
  <property name="dataSource">
<ref local="dataSource"/>
  </property>           
    </bean>
</beans>重点看注释部分,UserAction类继成了ActionSupport,当通过XXX.action访问UserAction中方法时,此时dataSource不为空,而Test类与struts没任何关系,就得不到dataSource的值了。说了这么多,我的问题是,有没办法,在Test类中,也能用到数据源dataSource呢?
并且,要求在applicationContext.xml设置,最好不要用hibernate。附:我现在用的方法是这样的:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource= (DataSource) context.getBean("dataSource");只是感觉这样很不好,我想直接在applicationContext.xml设置,在这里用get,set得到dataSource,怎么设置呢?水平有限,对spring也没什么研究,有高人懂不?欢迎指点。

解决方案 »

  1.   

    配置是没有问题的,问题出在你的调用上。struts和test都是由spring管理的,咋可能UserAction可以获得datasource,test得不到?
      

  2.   

    <bean id = "test" class = "com.test.Test"> 
          <property name="dataSource">
            <ref local="dataSource"/>
          </property>           
        </bean>        
    改成 <bean id = "test" class = "com.test.Test"> 
          <property name="dataSource">
            <ref bean="dataSource"/>
          </property>           
        </bean>  
    呢?
      

  3.   


    是这样子的,UserAction与Test类中,都有如下的方法: private DataSource dataSource = null;
    /**
     * @return the dataSource
     */
    public DataSource getDataSource()
    {
    return dataSource;
    } /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(DataSource dataSource)
    {
    this.dataSource = dataSource;
    }
    这样说吧,此时UserAction中,可以得到dataSource值,即不为空。
    而Test类中,dataSource值此为空,即setDataSource根本没把dataSource设置进来。也就是说,我也想在Test类中,也想要到dataSource值,怎么弄呢?(我那种方法除外)
    并非很明显,此时在applicationContext.xml设置的如下内容显然无效:
        <bean id = "test" class = "com.test.Test"> 
          <property name="dataSource">
            <ref local="dataSource"/>
          </property>           
        </bean>    
      

  4.   

    你的 配置 不是在一个文件内的,不用local吧
    ref 是全局的  local 是本文件的
      

  5.   

    说错了  bean 是全局的 local 是当前文件的
    如果注入的不是当前文件的bean  那就用<ref bean="dataSource"/>吧
      

  6.   

    你没有发现问题的关键,关键是你在使用action的时候,是你在web容器中加载了你的applicationContext.xml 文件,所以action中能够读到数据源,而test类你在运行的时候是不依赖web容器的,所以没有加载applicationContext.xml文件。你的配置文件正确的,要在test类里面使用数据源,那么就要在test类加载一下applicationContext.xml文件。比如String[] configLocations = { "spring/applicationContext-accountBind-client.xml"};
    ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
    不知道这样说你明白了没
      

  7.   

    难道楼主的Test是new 出来的?
      

  8.   


    不好意思,其实我本地就是用bean的了,写贴子时,拷错了。
    这样还是得不到值的
      

  9.   

    看一下你的Test有没有注入到Spring容器里,如果也注入了还得不到数据源传俺就不知道了
      

  10.   

    用JUnit测试 将DAO和Service配置文件读入 就可以了
      

  11.   

    你的Test 如果也是从容器里面取出来的
    那就不应该出问题的……不懂了,求高手
    同时学习
      

  12.   

    上面是笔误,此时应该是bean,不是local的。bean也是不行的。回9楼
    你说的有理,确实是这样的。我就是想知道,除了直接在类Test中加载applicationContext.xml,有没其它什么方法的了?能不能不用加Test中加载,通过写什么配置文件之类的解决呢?回10楼是的,你可以理解为我new 一个Test类,然后,想在里面处理一些数据库相关的业务。
      

  13.   

    得不到dataSource,应该就是没注入成功的。
    代码写是写了。
    再次更正下贴子中的代码:local改为bean
      

  14.   

    我理解你的TEST类就是junit之类的单元测试类,因为是单元测试,肯定是要运行这个类的时候加载的。你也可以写一个加载的类,比如使用jetty等来模拟web容器,在启动jetty的时候加载配置文件。其它的方法貌似没有了
      

  15.   

    你都不是从容器中得到的Test 怎么会注入source呢?
    所有通过spring ioc的对象都是由容器管理的
    你只有从容器中获取对象,属性的属性才是注入过的
    你直接new出来的,那肯定是null,这个对象不由spring容器管理,所以也不会注入
      

  16.   

    test对象要用spring的方法生成才能成功注入dataSource
      

  17.   

    test对象怎么用spring的方法生成?
    我的意思是,Test是一个独立的类,怎样才能跟spring扯上关系呢?失之成为spring容器中的一个对象。
      

  18.   

    你在xml文件里配了不就扯上关系了,我的意思 是你生成的test对象要通过spring的容器来生成。
      

  19.   

    Spring 中有个 ApplicationContextAware 配置一下就可以用了。写一个类 AppApplicationContextAware 实现 ApplicationContextAware 接口,然后配置到 Spring 中去:<bean id="appApplicationContextAware" class="xxx.xxx.xxx.AppApplicationContextAware" />然后通过 AppApplicationContextAware 就可以使用了。
    更详细地我也帮不了你什么了,我不大会 Spring
      

  20.   

    你是想不用new ClasspathXML....()吧?
    可以用 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext((ServletContext) ActionContext
                .getContext().get(ServletActionContext.SERVLET_CONTEXT));
     DataSource d= (DataSource ) ctx.getBean("dataSource ");
      

  21.   

    applicationContext.xml我估计是位置放错了
      

  22.   

    如果你的Test类没有通过spring的容器产生,那肯定不能得到spring产生的dataSource对象,在web环境中是通过web监听器来初始化spring容器的,在程序中,可以使用下面的语句得到web中的spring容器,再通过spring容器获得Test类的对象,这样,Test类对象的dataSource也就被spring注入了:
    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    Test test = (Test)context.getBean("test");
    test.function();//这时就可以在test的function中直接使用dataSource了。
    ......
      

  23.   

    首先感谢各位热情参与,虽然问题还是没解决,但还是有收获的,这里简单的小结下吧。一个普通的类,并不是说在applicationContext.xml配置了bean就能使用set/get方法实现注入的。
    火龙果的思路很好,他想到了,用某一特殊的类接口ApplicationContextAware,实现ApplicationContextAware对象的注入,进而间接的来获取数据源。但本人水平确实有限,测试了几次,还是没能得到ApplicationContext对象,进而求出数据源。ApplicationContextAware接口有一个方法是这样的:
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
    this.applicationContext=applicationContext;
    }
    所以,应该是这样方式可行的,只是可能我还有那里搞错了,先放着了,以后有空再研究了。25楼的那个方法似乎也不错,不用new对象,直接使用系统的静态类方法,但不知道为何,我本地好像有空指针现象,求不出的。具体那个静态类是空指针,还没去查,先不管了。其它的原因,9楼也说得很清楚的了,我不重复了。想来想去,最终还是先用自己的那个方法吧,先这样用的了,贴子明天再结吧。
      

  24.   


    试了下,你这种方法是可行的,其实也是跟我那个同类型的,跟25楼也同类型,只是25楼的出错了,呵
    但你这个不用new 对象,感觉还是比用new 的要好点吧。
      

  25.   

    不一样的,你的那个新产生了一个spring的IOC容器,还有可能由第二个spring产生多余的其他对象(两套单例对象)。
      

  26.   

    你把Test放在SPRING里,它不会给你自动管理的。STRUT的之所以能够,你可以参考其官方文档,有关与SPIRNG整合的文档说明。
      

  27.   

    不知道问题解决了没有?如果已经配置了,有没有写test的get和set方法?