SpringBoot beanが注入できない問題(パッケージの場所に関係)自動スキャンのパッケージを変更する

1997 ワード

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.SpringBootJdbcDemoApplication.SpringBootJdbcDemoApplication': Unsatisfied dependency expressed through field 'userRepository': No qualifying bean of type [com.example.repositories.UserRepository] found for dependency [com.example.repositories.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repositories.UserRepository] found for dependency [com.example.repositories.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)  
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)  

DaoのBeanに注入できません!解決分析後、SpringBootプロジェクトのBeanアセンブリのデフォルトルールは、Applicationクラスが存在するパッケージの位置に基づいて上から下にスキャンされていることがわかりました.「アプリケーションクラス」とは、SpringBootプロジェクトのエントリクラスを指します.このクラスの位置は重要です.アプリケーションクラスが存在するパッケージが:io.github.gefangshuai.appはioのみをスキャンする.github.gefangshuai.appパケットとそのすべてのサブパケットは、サービスまたはdaoが存在するパケットがioでない場合に使用する.github.gefangshuai.appとそのサブパッケージの下では、スキャンされません!
この点を知ることは非常に重要で、Springドキュメントに説明があるかどうか分からないが、知らなければ解決できない.回転:http://blog.csdn.net/gefangshuai/article/details/50328451,http://412887952-qq-com.iteye.com/blog/2292733開発ではSpring Bootが起動クラス同パッケージおよびサブパッケージ下の注釈をデフォルトでスキャンすることを知っていますが、このようなスキャンパッケージをどのように変更するかは、@ComponentScan注釈でスキャンするパッケージとスキャンするクラスを指定することです.
参照先:http://blog.csdn.net/u014695188/article/details/52263903