0

次のパッケージ階層があります
-org.bmark --
  dao
   ---実装
   ---interfaces --
  services
   ---実装
   ---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はUserServiceImplの実装に@Autowiredされており、すべてが正常に機能します。問題は、ContentTypeDAOもContentTypeServiceImplに@Autowiredされていることですが、サーバーを起動すると、次の例外が発生します。

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 に答える