Java 两个 jar 之间的 Spring Boot 和 @ComponentScan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22442822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot and @ComponentScan between two jars
提问by Muhi Masoud
I have 2 projects. One is a DAL project that does CRUD operations on a neo4j DB using spring neo4j APIs . This project is packaged as a jar and included in project #2. Project #2 Is a Spring restful services project that uses spring boot to package and create an executable jar that runes on an embedded tomcat server.
我有2个项目。一个是 DAL 项目,它使用 spring neo4j APIs 在 neo4j DB 上执行 CRUD 操作。该项目被打包为 jar 并包含在项目 #2 中。项目#2 是一个Spring RESTful 服务项目,它使用spring boot 打包并创建一个可执行的jar,该jar 运行在嵌入式tomcat 服务器上。
When trying to run my executable jar that Spring boot has created for me I keep getting this exception. expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
当尝试运行 Spring boot 为我创建的可执行 jar 时,我不断收到此异常。预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Based off of my reading if I am using @ComponentScan I can give the annotation directories to look in. So I give it the base dir for my services project. And I give it the base dir for my included DAL.jar but still no luck here is what the annotation looks like.
根据我的阅读,如果我使用 @ComponentScan,我可以提供注释目录来查看。所以我为它提供了我的服务项目的基本目录。我给它提供了我包含的 DAL.jar 的基本目录,但仍然没有运气这里是注释的样子。
Extracted from comments:
摘自评论:
Component scan declaration
组件扫描声明
@ComponentScan({"com.foo.dal.*","com.foo.notification.*"})
Stacktrace:
堆栈跟踪:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pushCommandsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.teradata.dal.example.PushRepository com.teradata.notification.rest.controller.PushCommandsController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.teradata.dal.example.PushRepository] 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)}
UPDATE:
更新:
based off of @chrylis answer: Made change to @ComponenetScan
基于@chrylis 的回答:已更改为@ComponenetScan
@ComponentScan({"com.teradata.notification","com.teradata.dal"})
running in to:
运行到:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration' is defined
MORE DETAIL ON THE DAL PROJECT AND THE SERVICE PROJECT:
DAL 项目和服务项目的更多细节:
DAL PROJECT:
达尔项目:
Services Project:
服务项目:
回答by chrylis -cautiouslyoptimistic-
The argument to @ComponentScan
is a package name, and those strings aren't valid packages. Drop the .*
from them; Spring scans subpackages automatically.
to 的参数@ComponentScan
是包名,这些字符串不是有效的包。.*
从他们那里删除;Spring 自动扫描子包。
回答by Tomek Samcik
Had this same issue for a short while, then @EntityScan
did the trick for me, just as adviced here - Spring Boot w/ JPA: move @Entity to different package.
有一段时间遇到了同样的问题,然后@EntityScan
为我做了这个技巧,就像这里的建议 - Spring Boot w/ JPA: move @Entity to different package。
Hope that helps
希望有帮助