2012-07-20 69 views
0

我有以下的包层次
-org.bmark
  --dao
    ---实现
    ---接口
  --services
    --- implementation
    --- interfaces
这是我的web.xml中春3 IOC注释

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/ApplicationContext.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

这是我的applicationContext.xml

<context:annotation-config/> 
<context:component-scan base-package="org.bmark.dao"/> 
<context:component-scan base-package="org.bmark.services"/> 

的一部分,我有一个的UserDAO和ContentTypeDAO(两者都实现)。我也有一个UserService和一个ContentTypeService(都有实现)。 services类具有@Service注释。
UserDAO是@Autowired到UserServiceImpl的实现中,并且一切正常。问题是,ContentTypeDAO也@Autowired到ContentTypeServiceImpl但是当我启动服务器我得到这个异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contentTypeServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.bmark.dao.intefaces.ContentTypeDAO org.bmark.services.implementations.ContentTypeServiceImpl.contentTypeDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.bmark.dao.intefaces.ContentTypeDAO] 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)}<br/> 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.bmark.dao.intefaces.ContentTypeDAO org.bmark.services.implementations.ContentTypeServiceImpl.contentTypeDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.bmark.dao.intefaces.ContentTypeDAO] 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)}<br/> 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.bmark.dao.intefaces.ContentTypeDAO] 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)} 

为什么我让我的ContentTypeDAO这个例外,而不是与我的UserDAO得到它? 我该如何解决这个问题?

回答

2

尝试

<context:component-scan base-package="org.bmark.dao, org.bmark.services"/> 

更换

<context:component-scan base-package="org.bmark.dao"/> 
<context:component-scan base-package="org.bmark.services"/> 

我不能完全肯定,如果上下文支持多个component-scan定义,或者也许一个覆盖另一个。