上代码吧。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的时候出现的问题。具体还要怎么写?
<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的时候出现的问题。具体还要怎么写?
<?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>
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");说实话 直接颠覆我的思想了。也让我意识到了耦合只是从一方面转向另一方面。
<property name="commodityDaoImpl" ref="commodityDaoImpl"></property>
</bean>你用spring注入的时候,你ref="commodityDaoImpl",这个commodityDaoImpl也是个bean啊,不知道你配了没?相对应应该有个接口实现才对。
<bean id="commodityAction" class="struts2.CommodityAction">
<property name="commodityDaoImpl" ref="commodityDaoImpl"></property>
</bean>lz你的commodityDaoImpl类在哪配置的?你用Junit测试 通过cxt.getBean("commodityDaoImpl")肯定得到的是null 还有你用spring@的方式 也很乱 要不就全部使用Spring@ 要不就全部配置
commodityDaoImpl 这个类在上面配置过也实现了没发出来,现在是注入不进去我全用的注释。。
发出来的基本就是全是那样写的。
我还以为必须new applicationcontext.xml 然后getbean手动一下。