文章目录
引言
Spring Boot 多模块项目跨包自动注入的方法,解决SpringBoot引用别的模块无法注入的问题。
I Spring Boot 多模块项目跨包自动注入的方法
1.1 问题描述
在使用 Maven 多模块开发的时候,A模块引入B模块,却无法注入B模块中被@Service、@Mapper、@Compoment、@Configuration 等注解修饰的类。
1.2 原因
SpringBootApplication启动类默认会扫描该启动类所在的包及其子包。
Spring Boot启动类通常使用@SpringBootApplication注解,该注解是一个组合注解,包含了@ComponentScan,@EnableAutoConfiguration和@Configuration等。
@ComponentScan会扫描该类所在的包及其子包中的Spring组件(如@Component, @Service, @Repository等),如果不指定basePackages,则默认会扫描该启动类所在的包及其子包。