您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何理解springboot加載META-INF/spring.factories方式”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何理解springboot加載META-INF/spring.factories方式”吧!
springboot 加載 META-INF/spring.factories
用戶應(yīng)用程序Application
建立META-INF/spring.factories文件的意義何在
平常我們?nèi)绾螌ean注入到容器當(dāng)中
ConfigurableApplicationContext context = SpringApplication.run(NacosSpringBootYamlApplication.class, args);
SpringApplication類(lèi)
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類(lèi)型(servlet、reactive、NoWeb) setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 這里會(huì)處理加載所有的spring.factories文件的內(nèi)容到緩存 找到*META-INF/spring.factories*中聲明的所有ApplicationContextInitializer的實(shí)現(xiàn)類(lèi)并將其實(shí)例化 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); //找到*META-INF/spring.factories*中聲明的所有ApplicationListener的實(shí)現(xiàn)類(lèi)并將其實(shí)例化 this.mainApplicationClass = deduceMainApplicationClass(); //獲得當(dāng)前執(zhí)行main方法的類(lèi)對(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類(lèi)型 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); } }
流程圖
@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注解在類(lèi)上進(jìn)行類(lèi)的注入
注意:
在我們主程序入口的時(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注解的入口程序類(lèi)所在的basepackage下的
所有帶有@Component注解的bean,從而注入到容器當(dāng)中。
但是
如果是加入maven坐標(biāo)依賴的jar包,就是項(xiàng)目根目錄以外的Bean是怎么添加的??
這個(gè)時(shí)候注解@EnableAutoConfiguration的作用就來(lái)了
導(dǎo)入了AutoConfigurationImportSelector這個(gè)類(lèi):
這個(gè)類(lèi)里面有一個(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類(lèi)名
為什么需要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ò)寫(xiě)/META-INF/spring.factories文件去進(jìn)行加載的。
到此,相信大家對(duì)“如何理解springboot加載META-INF/spring.factories方式”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。