0

我有以下包层次结构
-org.bmark
  --dao
   --- implementations
   --- interfaces
  --services
   --- implementations
   --- interfaces
这是我的 web.xml

<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 (两者都有实现)。服务类具有 @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 却没有?我该如何解决?

4

1 回答 1

2

尝试更换

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

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

我不完全确定上下文是否支持多个component-scan定义,或者一个定义是否覆盖另一个。

于 2012-07-20T17:01:37.993 回答