应该没有少包吧
问题解决了嘛?你这是采用set函数进行自动注入是吗?如果是请确认你的set函数是否正确编写。
下面贴一个,使用set函数注入的正确示例
方式一:配置注入 <!--set函数注入--> <bean id="injectionServiceSet" class="com.imooc.ioc.injection.service.InjectionServiceImpl"> <property name="injectionDAO" ref="injectionDAO"></property> </bean> <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean> //DAO接口 public interface InjectionDAO { void save(String arg); } //DAO实现类 public class InjectionDAOImpl implements InjectionDAO{ @Override public void save(String arg) { System.out.println(arg); } } //Service接口 public interface InjectionService { void saveService(String arg); } //Service实现类 public class InjectionServiceImpl implements InjectionService { InjectionDAO injectionDAO; public void setInjectionDAO(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } @Override public void saveService(String arg) { injectionDAO.save(arg); } } //测试代码 @Test public void testSetter() { InjectionService service = super.getBean("injectionServiceSet"); service.saveService("这是要保存的数据"); } 方式二:通过使用注解自动注入 配置 <context:component-scan base-package="com.imooc.annotation"></context:component-scan> //DAO加上注解 @Repository public class InjectionDAOImpl implements InjectionDAO { @Override public void save(String arg) { System.out.println(arg); } } //Service实现类加上注解 @Service public class InjectionServiceImpl implements InjectionService { InjectionDAO injectionDAO; @Autowired public void setInjectionDAO(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } @Override public void saveService(String arg) { injectionDAO.save(arg); } } 其他的和“方式一”一样,Good Luck!
有没有声明成员,或者说需要注入的对象