多模块项目的 SpringBoot ComponentScan 问题 [英] SpringBoot ComponentScan issue with multi-module project
问题描述
我有一个带有 myapp-core 和 myapp-web 模块的 myapp 父 pom 类型 maven 项目.myapp-core 模块作为依赖添加到 myapp-web.
I have a myapp parent pom type maven project with myapp-core and myapp-web modules. myapp-core module is added as dependency to myapp-web.
myapp-core 模块中的所有类都驻留在根包 com.myapp.core 中,而 myapp-web 模块中的所有类都驻留在根包 com.myapp.web 中>
All the classes in myapp-core module reside in root package com.myapp.core and all classes in myapp-web module reside in root package com.myapp.web
主 Application.java 也在 com.myapp.web 包中.由于我的核心模块根包不同,我为 ComponentScan 包含了通用基础包 "com.myapp" 如下:
The main Application.java is also in com.myapp.web package. As my core module root package is different I am including common base package "com.myapp" for ComponentScan as follows:
@Configuration
@ComponentScan(basePackages="com.myapp")
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
现在令人惊讶的是,如果我使用 Run As -> Spring Boot App 运行此应用程序,它运行良好.但是,如果我将它作为 Run As -> Java Application 运行,它会失败并显示错误,说它找不到 myapp-core 模块中定义的 bean.
Now the surprising thing is if I run this app using Run As -> Spring Boot App it is working fine. But if I run it as Run As -> Java Application it is failing with error saying it can't found beans defined in myapp-core module.
如果我将我的 Application.java
移动到 com.myapp
包,它工作正常.即使我也将它作为 Java 应用程序运行,它也应该可以工作,对吗?
If I move my Application.java
to com.myapp
package it is working fine.
It should work even if i run it as Java Application also, right?
推荐答案
在为 spring 启用调试日志级别并查看大量日志后,我发现对 JPA 存储库、JPA 实体等各种组件的扫描取决于 Application.java 的包名.
After enabling debug log level for spring and going through extensive logs I found that scanning for various components like JPA Repositories, JPA Entities etc are depending on the Application.java's package name.
如果 JPA 存储库或实体不在 Application.java
包的子包中,那么我们需要如下明确指定它们:
If the JPA Repositories or Entities are not in sub packages of Application.java
's package then we need to specify them explicitly as follows:
@Configuration
@ComponentScan(basePackages="com.sivalabs.jcart")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages="com.sivalabs.jcart")
@EntityScan(basePackages="com.sivalabs.jcart")
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
通过上述附加的 @EnableJpaRepositories
,@EntityScan
我可以使用 Run As -> Java Application 运行它.
With the above additional @EnableJpaRepositories
, @EntityScan
I am able to run it using Run As -> Java Application.
但仍然不确定 Run As -> Spring Boot App 时它是如何正常工作的!!
But still not sure how it is working fine when Run As -> Spring Boot App!!
无论如何我认为将我的 Application.java
移动到 com.myapp
包比与 SpringBoot 对抗更好!
Anyway I think it is better to move my Application.java
to com.myapp
package rather than fighting with SpringBoot!
这篇关于多模块项目的 SpringBoot ComponentScan 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!