以下两步都要满足才能注入到容器中:
查看@SpringBootApplication注解的scanBasePackages属性中,是否加入了所有需要扫描的路径。
例如:
@SpringBootApplication(scanBasePackages = {"com.module1", "com.module2"})
多模块的pom文件,是否是集成关系。
例如,@SpringBootApplication注解对应的启动类App在module3中,那么如果需要将module1和module2中的bean注入到module3中的话,有两种解决方案。
需要在module3的pom.xml文件中做如下配置:
- <dependencies>
- <dependency>
- <groupId>com.testgroupId>
- <artifactId>module1artifactId>
- dependency>
- dependencies>
-
- <dependencies>
- <dependency>
- <groupId>com.testgroupId>
- <artifactId>module2artifactId>
- dependency>
- dependencies>
或者是在module2的pom.xml文件和module3的pom.xml分别添加,并形成链路。
module2的pom.xml如下:
- <dependencies>
- <dependency>
- <groupId>com.testgroupId>
- <artifactId>module1artifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
相应的,module3的pom.xml如下:
- <dependencies>
- <dependency>
- <groupId>com.testgroupId>
- <artifactId>module2artifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- dependencies>
这样便可以形成链路,使得module1、module2、module3的bean都能够注入到容器中。