Java 如何在包含的 JAR 中制作 Spring 的 @ComponentScan 搜索组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28679581/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:37:47  来源:igfitidea点击:

How to make Spring's @ComponentScan search components in included JARs

javaspringspring-mvcspring-boot

提问by Miciurash

I made a jar with Spring components to include in multiple projects (core.jar).

我用 Spring 组件制作了一个 jar 以包含在多个项目 (core.jar) 中。

I created a new Spring project, have @ComponentScan to the correct package, but it is not aware of the components in the jar.

我创建了一个新的 Spring 项目,将 @ComponentScan 设置为正确的包,但它不知道 jar 中的组件。

How to make Spring's @ComponentScan search components in included JARs ?

如何在包含的 JAR 中制作 Spring 的 @ComponentScan 搜索组件?

采纳答案by Sotirios Delimanolis

Give it the appropriate package name to scan in the JAR.

为其指定适当的包名称以在 JAR 中进行扫描。

@ComponentScan(basePackages = {"com.example.from.jar"})

回答by Ebrahim Salehi

I had a similar issue with Spring boot and @ComponentScan and this document helped me solving the issue: http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html.

我在 Spring boot 和 @ComponentScan 上遇到了类似的问题,这个文档帮助我解决了这个问题:http: //docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-代码.html

The point was that my @SpringBootApplicationclass was in the root package of one of basePackages of @ComponentScan. After I moved it to a subpackage, Spring was able to recognize all @ComponentScans.

关键是我的@SpringBootApplication课程位于 .basePackages 之一的根包中@ComponentScan。在我把它移到一个子包之后,Spring 能够识别所有的@ComponentScans。