上代码吧。applicationcontext.xml<bean id="commodityAction" class="struts2.CommodityAction">
<property name="commodityDaoImpl" ref="commodityDaoImpl"></property>
</bean>java commodityAction.javapublic CommodityDaoImpl getCommodityDaoImpl() {
return commodityDaoImpl;
}
@Resource(name="commodityDaoImpl")
public void setCommodityDaoImpl(CommodityDaoImpl commodityDaoImpl) {
this.commodityDaoImpl = commodityDaoImpl;
}这样按理说只要tomcat启动时候就可以自动注入并且使用。然而这样在junit中测试是nulljunti这样写这样的话就可以接到为什么呢???
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.getBean("commodityDaoImpl");这个是整合struts2和srping的时候出现的问题。具体还要怎么写?

解决方案 »

  1.   


    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    default-lazy-init="true"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
    <property name="url"
    value="jdbc:sqlserver://*.*.*.*:1433;DatabaseName=***"></property>
    <property name="username" value="***"></property>
    <property name="password" value="***"></property>
    <property name="initialSize" value="5" />
    <property name="maxActive" value="100" />
    <property name="maxIdle" value="30" />
    <property name="maxWait" value="1000" />
    <property name="defaultAutoCommit" value="true" />
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">false</prop>
    </props>
    </property>
    <property name="mappingResources">

    </property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:advice transaction-manager="hibernateTransactionManager"
    id="txAdvice">
    <tx:attributes>
    <tx:method name="*" propagation="REQUIRED"></tx:method>
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="myPoint" expression="execution(* com.eps.service..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint" />
    </aop:config>
    <bean id="dao" class="com.eps.dao.Dao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
      

  2.   

    我现在是想把东西new然后在ref注入到另外一个java文件中 最后直接调用就可以。这个xml中到底还要怎么配置?????或者java 中还要怎么写?? getset方法都有 注解注解到了set方法上。
      

  3.   

    二楼 你说的意思难道是必须配置事物才能实现自动new?  我这个问题应该是注入不进去
      

  4.   

    必须使用getbean1 .WebApplicationContextUtils     .getRequiredWebApplicationContext(servlet.getServletContext());   
    public Object getBean(String name)   
       {   
           if (ctx == null)   
           {   
               ctx = WebApplicationContextUtils   
                     .getRequiredWebApplicationContext(servlet.getServletContext());   
           }   
           return ctx.getBean(name);   
       }
    2. public static void main(final String[] args) throws Exception {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class); //必需放在WEB-INF/lib/class下面
            FooService fooService = (FooService) ctx.getBean("fooService");
            fooService.insertFoo (new Foo());
        }3. ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "src/cn/com/jbaptech/po/applicationContext.xml" });ctx.getBean("fooService");说实话 直接颠覆我的思想了。也让我意识到了耦合只是从一方面转向另一方面。
      

  5.   

    注入就是为了让每个层都低耦合,不需要去new的,你new了的话,注入就没用了
      

  6.   

    <bean id="commodityAction" class="struts2.CommodityAction">
    <property name="commodityDaoImpl" ref="commodityDaoImpl"></property>
    </bean>你用spring注入的时候,你ref="commodityDaoImpl",这个commodityDaoImpl也是个bean啊,不知道你配了没?相对应应该有个接口实现才对。
      

  7.   

    applicationcontext.xml
    <bean id="commodityAction" class="struts2.CommodityAction">
    <property name="commodityDaoImpl" ref="commodityDaoImpl"></property>
    </bean>lz你的commodityDaoImpl类在哪配置的?你用Junit测试 通过cxt.getBean("commodityDaoImpl")肯定得到的是null 还有你用spring@的方式 也很乱 要不就全部使用Spring@ 要不就全部配置 
      

  8.   

    额  我也刚学spring  使用junit测试的时候使用了new的方式new出XML。
    commodityDaoImpl 这个类在上面配置过也实现了没发出来,现在是注入不进去我全用的注释。。
    发出来的基本就是全是那样写的。
    我还以为必须new applicationcontext.xml 然后getbean手动一下。