准备知识:SPI(Service Provider Interface)
可参考另一篇博文
我使用spring.factories的场景一般如下:
在不改变原有Spring应用的前提条件下(比如增加新的扫描包路径,配置文件等等),想让Spring可以扫描到你新增加jar包中定义的Component,就需要在新增的jar包中使用spring.factories。
===具体使用========================================
定义@Configuration
@Configuration
@EntityScan(basePackages = "")
@EnableJpaRepositories("")
@ComponentScan(basePackages = "")
@PropertySource(value = {"classpath:/testa.properties"}, encoding = "utf-8")
public class MyConfiguration {
}
定义spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.etoak.config.MyConfiguration
这样Spring Boot就可以扫描其包以外的Bean了。
====================================

本文介绍了如何在不修改Spring应用原有配置的情况下,通过spring.factories文件让Spring扫描到新增jar包中的Component。具体操作包括定义@Configuration类,配置@EntityScan、@EnableJpaRepositories、@ComponentScan和@PropertySource,并在spring.factories中指定@EnableAutoConfiguration以启用自定义配置。此方法常用于SpringBoot应用,使得Spring能够识别和加载外部Bean。
最低0.47元/天 解锁文章
7405

被折叠的 条评论
为什么被折叠?



