如何在包含的JAR中创建Spring的@ComponentScan搜索组件

Mic*_*ash 10 java spring spring-mvc spring-boot

我用Spring组件创建了一个包含在多个项目中的jar(core.jar).

我创建了一个新的Spring项目,将@ComponentScan放到正确的包中,但是它不知道jar中的组件.

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

Sot*_*lis 18

为其提供适当的包名称以在JAR中扫描.

@ComponentScan(basePackages = {"com.example.from.jar"})
Run Code Online (Sandbox Code Playgroud)

  • @Miciurash _it不起作用_不是很有用.编辑您的问题并提供详细信息.我的回答是回答你当前的问题:_如何在包含的JAR中创建Spring的@@ ComponentScan`搜索组件?_这就是你的做法.如果它不起作用,那就是你做错了,你需要提供你的代码/配置来重现你的问题. (2认同)

小智 6

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

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