springboot 加載 META-INF/spring.factories方式
springboot 加載 META-INF/spring.factories
用戶應(yīng)用程序Application
ConfigurableApplicationContext context = SpringApplication.run(NacosSpringBootYamlApplication.class, args);
SpringApplication類
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); }
// 這里Class是數(shù)組 public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args); }
public SpringApplication(Class<?>... primarySources) { this(null, primarySources); }
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); // 這里就是SpringMvcApplication的實(shí)例 this.webApplicationType = WebApplicationType.deduceFromClasspath();// deduce(推斷)web類型(servlet、reactive、NoWeb) setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 這里會(huì)處理加載所有的spring.factories文件的內(nèi)容到緩存 找到*META-INF/spring.factories*中聲明的所有ApplicationContextInitializer的實(shí)現(xiàn)類并將其實(shí)例化 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); //找到*META-INF/spring.factories*中聲明的所有ApplicationListener的實(shí)現(xiàn)類并將其實(shí)例化 this.mainApplicationClass = deduceMainApplicationClass(); //獲得當(dāng)前執(zhí)行main方法的類對(duì)象,這里就是SpringMvcApplication的實(shí)例 }
具體加載該classLoader下的所有spring.factories到緩存
如果緩存已經(jīng)存在,則直接根據(jù)key,返回?cái)?shù)據(jù)
/** key:是spring.factories的key value:是根據(jù)key分組,把同個(gè)key的不同value放到list里面 */ private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { //已經(jīng)處理過(guò)了 直接返回 return result; } //url: // file:/C:/Users/kongqi/.m2/repository/org/springframework/spring-beans/5.1.9.RELEASE/spring-beans-5.1.9.RELEASE.jar!/META-INF/spring.factories try { //得到classloader下的所有jar包中的spring.factories的文件 Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); // 得到spring.factories的內(nèi)容 for (Map.Entry<?, ?> entry : properties.entrySet()) { // key: spring.factories的key value: spring.factories的value String factoryClassName = ((String) entry.getKey()).trim(); // spring.factories的key for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {//value根據(jù)逗號(hào),分隔 result.add(factoryClassName, factoryName.trim()); //factoryClassName其實(shí)就是spring.factories的key 由于value是List類型 MultiValueMap value有多個(gè) } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
流程圖
建立META-INF/spring.factories文件的意義何在
平常我們?nèi)绾螌ean注入到容器當(dāng)中
@Configuration @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService() { HelloService service = new HelloService(); service.setHelloProperties( helloProperties ); return service; } }
一般就建立配置文件使用@Configuration,里面通過(guò)@Bean進(jìn)行加載bean
或者使用@Compont注解在類上進(jìn)行類的注入
注意:
在我們主程序入口的時(shí)候:
@SpringBootApplication這個(gè)注解里面的東西
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {
里面注解@EnableAutoConfiguration
@ComponentScan注解指掃描@SpringBootApplication注解的入口程序類所在的basepackage下的
所有帶有@Component注解的bean,從而注入到容器當(dāng)中。
但是
如果是加入maven坐標(biāo)依賴的jar包,就是項(xiàng)目根目錄以外的Bean是怎么添加的??
這個(gè)時(shí)候注解@EnableAutoConfiguration的作用就來(lái)了
導(dǎo)入了AutoConfigurationImportSelector這個(gè)類:
這個(gè)類里面有一個(gè)方法
/** * Return the auto-configuration class names that should be considered. By default * this method will load candidates using {@link SpringFactoriesLoader} with * {@link #getSpringFactoriesLoaderFactoryClass()}. * @param metadata the source metadata * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation * attributes} * @return a list of candidate configurations */ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; }
@EnableAutoConfiguration注解來(lái)注冊(cè)項(xiàng)目包外的bean。而spring.factories文件,則是用來(lái)記錄項(xiàng)目包外需要注冊(cè)的bean類名
為什么需要spring.factories文件,
因?yàn)槲覀冋麄€(gè)項(xiàng)目里面的入口文件只會(huì)掃描整個(gè)項(xiàng)目里面下的@Compont @Configuration等注解
但是如果我們是引用了其他jar包,而其他jar包只有@Bean或者@Compont等注解,是不會(huì)掃描到的。
除非你引入的jar包沒(méi)有Bean加載到容器當(dāng)中
所以我們是通過(guò)寫/META-INF/spring.factories文件去進(jìn)行加載的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC空指針異常NullPointerException解決及原理解析
這篇文章主要介紹了SpringMVC空指針異常NullPointerException解決及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java自定義類數(shù)組報(bào)null的相關(guān)問(wèn)題及解決
這篇文章主要介紹了Java自定義類數(shù)組報(bào)null的相關(guān)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09IDEA連接MySQL數(shù)據(jù)庫(kù)的4種方法圖文教程
IDEA是一種流行的Java開發(fā)工具,可以方便地連接MySQL,這篇文章主要給大家介紹了關(guān)于IDEA連接MySQL數(shù)據(jù)庫(kù)的4種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的常用方法
在Spring Boot應(yīng)用中連接多個(gè)數(shù)據(jù)庫(kù)或數(shù)據(jù)源可以使用多種方式,本文講給大家介紹兩種常用的方法:使用Spring Boot官方支持的多數(shù)據(jù)源配置和使用第三方庫(kù)實(shí)現(xiàn)多數(shù)據(jù)源,文章通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的方法
MyBatis-Plus是一款基于MyBatis的增強(qiáng)工具,它提供了一些便捷的功能和增強(qiáng)的查詢能力,數(shù)據(jù)權(quán)限控制是在系統(tǒng)中對(duì)用戶訪問(wèn)數(shù)據(jù)進(jìn)行限制的一種機(jī)制,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的相關(guān)資料,需要的朋友可以參考下2024-01-01