Spring Boot 之 spring.factories
转载至 https://www.cnblogs.com/huanghzm/p/12217630.html
1、抛出一个问题
首先抛出一个问题:如果想要被Spring容器管理的Bean的路径不再Spring Boot 的包扫描路径下,怎么办呢?也就是如何去加载第三方的Bean 呢?
2、解决办法有两个
2.1 使用 @Import 注解
配合@Configuration 来使用,(同样的话这里可以单独制定扫描的路径,然后配合@Component注解)
@Import({SwaggerConfig.class}) @Configuration public class MyConfig { @Bean public Person person(){ return new Person(); } @Bean public PesonPointcut pesonPointcut(){ return new PesonPointcut(); } }
直接在启动类上使用@Import注解,因为@SpringBootApplication 底层注解也包含了@Configuration
/** * author gaohq */ @Import({GaohqClass.class}) @SpringBootApplication @ImportResource("classpath:META-INF/spring/*.xml") public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
2.2 使用spring.factories 文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ali.gts.sofa.consumer.SwaggerConfig
3、spring.factories 的位置
META-INF/spring.factories