我正在尝试创建spring-mvc项目。
我的“存储库”组件定义为
@Repository("testEntityDao")
public class TestEntityDaoImpl extends GenericDaoImpl<TestEntity> implements TestEntityDao {
具有TestEntityDao实例的“Service”组件
@Service("testManager")
public class TestManagerServiceImpl implements TestManagerService{
@Autowired
TestEntityDao testEntityDao;
TestManagerServiceImpl没有定义任何构造函数,也没有testEntityDao的getter和setter。 (我在编写setter之后尝试了代码,但是得到了同样的错误)。
ApplicationContext.xml具有以下行以启用按名称自动装配
<beans xmlns="http://www.springframework.org/schema/beans"
.
.
default-autowire="byName">
<context:annotation-config />
<context:component-scan
base-package= .... />
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我得到的错误是
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.cms.dao.TestEntityDao com.test.cms.service.impl.TestManagerServiceImpl.testEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.cms.dao.TestEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=testEntityDao)}
如何解决此错误?
答案 0 :(得分:2)
只需引用通用父包而不是所有这些包。将组件扫描为:
<context:component-scan base-package="com.test.cms"/>