我有两个项目。 一个是DAL项目,使用Spring Neo4j API对Neo4j数据库执行CRUD操作。这个项目被打包成jar并包含在项目#2中。 项目#2是一个Spring RESTful服务项目,使用Spring Boot将其打包成可执行的jar文件,该文件在嵌入式Tomcat服务器上运行。
当我尝试运行Spring Boot为我创建的可执行jar时,我总是会得到这个异常。 预期至少有1个bean符合此依赖项的自动装配候选人。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
根据我的阅读,如果我使用@ComponentScan,我可以给注释指定要查找的目录。所以我给了它我的服务项目的基本目录。我还给了它我的包含的DAL.jar的基本目录,但仍然没有运气。以下是注释的样子。
从注释中提取的Component Scan声明:
@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答案进行修改: 对@ComponentScan进行更改
@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项目:
服务项目: