--以下为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;
}}

解决方案 »

  1.   

    -- 如果用ApplicationContext apc = new ClassPathXmlApplicationContext("applicationContext.xml");
    会不会是路径的问题呢
      

  2.   

    楼主你错了,action中private TestService testService;这个是不对的。改成这样就可以了 private TestServiceImpl testService;你注入的是service实现类而不是接口,所以在action中应该用实现类而不是接口
      

  3.   


     朋友有点没明白...  能说的具体些吗?你看我如果加上
    <beans default-autowire="byName"><bean id="testService" class="orm.base.service.impl.TestServiceImpl">
    <property name="dao">
    <ref bean="daoImpl" />
    </property>
    </bean>这样的话Action中testService就可以注入,这不同样是spring里面注入吗,有点晕了.....
      

  4.   

    加上这句代码注入是好用的。   我说的是如果不加这句代码,spring自动注入
      

  5.   

    http://sdmirzlp195882.sdo.blog.163.com/blog/static/124219009200982292837955/?fromdm&fromSearch&isFromSearchEngine=yes
      

  6.   

    楼主还是好好的去看看spring注入吧,<beans default-autowire="byName">,这个是根据名字自动装配,还有一种就是根据类型自动装配,另外就是你第一次发的那种注入,总共是3种,是不同的。