Problem Description
When using Maven
multi- module development, module A imports module B, but cannot be injected into module B by @Service
, @Mapper
, @Compoment
, @Configuration
etc. As long as you can think of annotations that can place classes in Spring
containers, they cannot be injected into module A. .
parent project
<modules>
<module>blog</module>
<module>admin</module>
<module>framework</module>
</modules>
Directory Structure
Blog
module import framework
module
<dependencies>
<dependency>
<groupId>com.hsqyz</groupId>
<artifactId>framework</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
But after the Blog project is started, the Bean of the framework module cannot be scanned...
solution one
Make sure that the startup class package paths of the two modules are consistent:
blog module package path: com.hsqyz
framework module package path:com.hsqyz
solution two
Use @SpringBootApplication
the scanBasePackages
attribute to specify the scan path of the package
@SpringBootApplication(scanBasePackages = {
"com.hsqyz.blog","com.hsqyz.framework"})
Or use @ComponentScan
the value
attribute to specify the scan path for the package
@ComponentScan(value = {
"com.hsqyz.blog","com.hsqyz.framework"})