Spring Boot 工程作为 SDK 依赖库方式自动注入 Bean 类

Spring Boot About 979 words

需求

抽取公共模块,以SDK的方式被宿主引入。在这个SDK中,自动注入@Configuration@Bean@Component@Service@Repository@Controller等在SDK工程中定义的Bean类。

Bean 类

所有Bean类都位于com.example.service

自动配置类

@ComponentScan(basePackages = {"com.example.service"})
@AutoConfiguration
public class MyAutoConfiguration {
}

Spring Boot 2.7 及 3

└── resources
    ├── META-INF
    │              └── spring
    │                  └── org.springframework.boot.autoconfigure.AutoConfiguration.imports

org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中添加com.example.MyAutoConfiguration全路径类名。

com.example.app.MyConfiguration
com.example.app.MyConfiguration2

Spring Boot 2.7 以下

└── resources
    ├── META-INF
    │              └── spring.factories

spring.factories文件内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.app.MyConfiguration, \
com.example.app.MyConfiguration2

参考链接

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#changes-to-auto-configuration

Views: 275 · Posted: 2024-03-28

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh