两个罐子之间的Spring Boot和@ComponentScan [英] Spring Boot and @ComponentScan between two jars
问题描述
我有2个项目。
一个是DAL项目,它使用spring neo4j API对neo4j数据库执行CRUD操作。该项目打包为jar并包含在项目#2中。
Project#2是一个Spring restful服务项目,它使用spring boot打包并创建一个可嵌入tomcat服务器上的可执行jar。
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.
当试图运行Spring引导为我创建的可执行jar,我一直得到这个异常。
预计至少有1个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
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)}
根据我的阅读,如果我使用@ComponentScan我可以给注释目录查看。所以我给它我的服务项目的基础目录。我给它包含了DAL.jar的基础目录,但是这里的注释仍然没有运气。
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({"com.foo.dal.*","com.foo.notification.*"})
Stacktrace:
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)}
更新:
基于@chrylis回答:
更改为@ComponenetScan
based off of @chrylis answer: Made change to @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项目和服务项目的详细信息:
MORE DETAIL ON THE DAL PROJECT AND THE SERVICE PROJECT:
DAL PROJECT:
DAL PROJECT:
服务项目:
推荐答案
@ComponentScan <的参数/ code>是包名称,这些字符串不是有效的包。从中删除
。*
; Spring会自动扫描子包。
The argument to @ComponentScan
is a package name, and those strings aren't valid packages. Drop the .*
from them; Spring scans subpackages automatically.
这篇关于两个罐子之间的Spring Boot和@ComponentScan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!