spring注入,但action中service为空,附代码 求帮助
--以下为spring注入
<bean id="daoImpl" class="orm.base.DAOImpl" >
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="testServiceImpl" class="orm.base.service.impl.TestServiceImpl">
<property name="dao">
<ref bean="daoImpl" />
</property>
</bean>
<bean id="testAction" class="orm.base.action.TestAction">
<property name="testService" ref="testServiceImpl"></property>
</bean>
-- 接口
public interface TestService {
public void save(Sshtable s);
}
-- 实现类
public class TestServiceImpl implements TestService {
private DAO dao;
public DAO getDao() {
return dao;
}
public void setDao(DAO dao) {
this.dao = dao;
}
public void save(Sshtable s) {
this.getDao().save(s);
}
}
-- Action中 testService为空
-- 如果用ApplicationContext apc = new ClassPathXmlApplicationContext("applicationContext.xml");
testService = (TestService)apc.getBean("testServiceImpl");
这样就ok
public class TestAction extends BaseAction{
private static final long serialVersionUID = 1L;
private TestService testService;
public String execute(){
testService.save(Object);
return null;
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
}
------解决方案--------------------楼主还是好好的去看看spring注入吧,<beans default-autowire="byName">,这个是根据名字自动装配,还有一种就是根据类型自动装配,另外就是你第一次发的那种注入,总共是3种,是不同的。