弹簧靴和两个罐子之间的@ComponentScan

2022-09-03 01:43:09

我有2个项目。一个是DAL项目,它使用弹簧neo4j API在neo4j DB上进行CRUD操作。此项目被打包为 jar 并包含在项目 #2 中。项目#2是一个Spring restful服务项目,它使用spring boot来打包和创建一个在嵌入式tomcat服务器上符文的可执行jar。

当尝试运行Spring boot为我创建的可执行jar时,我不断收到此异常。预计至少有 1 个 bean 有资格作为此依赖项的自动连接候选项。Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

根据我的阅读,如果我使用@ComponentScan我可以给出注释目录来查找。因此,我为它提供了我的服务项目的基本目录。我为它提供了我所包含的DAL的基本目录.jar但在这里仍然没有运气,这就是注释的样子。

摘自评论:

组件扫描声明

@ComponentScan({"com.foo.dal.*","com.foo.notification.*"})

堆栈跟踪:

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)}

更新:

基于@chrylis答案:对@ComponenetScan进行了更改

@ComponentScan({"com.teradata.notification","com.teradata.dal"})

磨合到:

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

有关 DAL 项目和服务项目的更多详细信息:

DAL项目:

DAL project structure


DAL classes


DAL classes


DAL classes


DAL classes


服务项目:

Service project structure


Service classes


Service classes


Service classes


答案 1

参数 to 是包名称,这些字符串不是有效的包。从他们那里删除;Spring 自动扫描子包。@ComponentScan.*


答案 2

有一小段时间有同样的问题,然后为我做了这个技巧,就像这里的建议 - Spring Boot w / JPA:@Entity移动到不同的软件包@EntityScan

希望有所帮助


推荐