Solve the problem that SpringBoot references other modules that cannot be injected

Problem Description

When using Mavenmulti- module development, module A imports module B, but cannot be injected into module B by @Service, @Mapper, @Compoment, @Configurationetc. As long as you can think of annotations that can place classes in Springcontainers, they cannot be injected into module A. .


parent project

<modules>
	<module>blog</module>
	<module>admin</module>
	<module>framework</module>
</modules>

Directory Structure


Blogmodule import frameworkmodule

<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 @SpringBootApplicationthe scanBasePackagesattribute to specify the scan path of the package

@SpringBootApplication(scanBasePackages = {
    
    "com.hsqyz.blog","com.hsqyz.framework"})

Or use @ComponentScanthe valueattribute to specify the scan path for the package

@ComponentScan(value = {
    
    "com.hsqyz.blog","com.hsqyz.framework"})

Guess you like

Origin blog.csdn.net/qq_31762741/article/details/126563900