国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Springboot自動加載配置的原理解析

Springboot自動加載配置的原理解析

2022-02-21 13:15好煩吃不胖 Java教程

Springboot遵循“約定優(yōu)于配置”的原則,使用注解對一些常規(guī)的配置項做默認配置,減少或不使用xml配置,讓你的項目快速運行起來,這篇文章主要給大家介紹了關(guān)于Springboot自動加載配置原理的相關(guān)資料,需要的朋友可以參考下

 

1、springboot自動配置的原理初探

以下注解都在springboot的自動化配置包中:spring-boot-autoconfigure。讀者朋友可以跟著一下步驟走一遍,應(yīng)該對自動配置就有一定的認知了。

1.springboot程序的入口是在啟動類,該類有個關(guān)鍵注解SpringBootApplication

@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 {
    //略……
}

2.打開SpringBootApplication注解,上面有個關(guān)鍵注解EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    //……
}

3.EnableAutoConfiguration上有個@Import(AutoConfigurationImportSelector.class),注意AutoConfigurationImportSelector,

@Import作用創(chuàng)建一個AutoConfigurationImportSelector的bean對象,并且加入IoC容器

	//org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
//此處只貼了關(guān)鍵方法
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;
	}

4.AutoConfigurationImportSelector類中的getCandidateConfigurations方法代碼如上,其調(diào)用了SpringFactoriesLoader的loadFactoryNames方法,來獲取

configurations,此configurations列表其實就是要被自動花配置的類。SpringFactoriesLoader的兩個重要方法如下:

//org.springframework.core.io.support.SpringFactoriesLoader
//只貼了兩個關(guān)鍵方法
	public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

//此方法返回的是即將要被自動化配置的類的全限定類名,是從META-INF/spring.factories配置的,配置文件中有個org.springframework.boot.autoconfigure.EnableAutoConfiguration 其后面可配置多個想被自動花配置的類
    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullab等le ClassLoader classLoader) {
            String factoryTypeName = factoryType.getName();
            return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
     }


	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		try {
			Enumeration<URL> urls = (classLoader != null ?
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));//META-INF/spring.factories
			result = new LinkedMultiValueMap<>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					String factoryTypeName = ((String) entry.getKey()).trim();
					for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
						result.add(factoryTypeName, factoryImplementationName.trim());
					}
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

5.舉例分析,我們在spring.factories中可以看到org.springframework.boot.autoconfigure.EnableAutoConfiguration后有一個org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,說明springboot希望redis能夠自動化配置。接著我們打開RedisAutoConfiguration源碼查看。此處我故意沒復(fù)制源碼,用的截圖,可以看到截圖直接有報錯,編譯錯誤,錯誤的原因是我們還沒添加spring-boot-starter-data-redis的依賴。**這里有個問題,為什么明明代碼都報錯,Cannot resolve symbol xxx(未找到類),但是我們的項目依然可以啟動?不信你建立一個簡單的springboot項目,只添加web依賴,手動打開RedisAutoConfiguration,發(fā)現(xiàn)是報紅錯的,但是你啟動項目,發(fā)現(xiàn)沒任何問題,why??**這個問題后面再解答,先接著看自動配置的問題。

Springboot自動加載配置的原理解析

6.先把RedisAutoConfiguration源碼復(fù)制出來方便我寫注釋,上面用截圖主要是讓大家看到報錯

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

}

看源碼可知RedisAutoConfiguration上有一個Configuration和ConditionalOnClass注解,先分析這兩個。首先Configuration注解,代表這是個Java config配置類,和spring配置bean的xml文件是一個作用,都是用來實例化bean的,**但是注意還有個@ConditionalOnClass(RedisOperations.class)注解,這個注解的作用是當RedisOperations.class這個類被找到后才會生效,如果沒找到此類,那么整個RedisAutoConfiguration就不會生效。**所以當我們引入了redis的依賴,springboot首先會通過RedisAutoConfiguration的方法redisTemplate給我們設(shè)置一個默認的redis配置,當然這個方法上也有個注解@ConditionalOnMissingBean(name = "redisTemplate"),就是當我們沒有手動配redisTemplate這個bean它才會調(diào)用這個默認的方法,注入一個redisTemplate到IoC容器,所以一般情況我們都是手動配置這個redisTemplate,方便我們設(shè)置序列化器,如下:

@Configuration
public class RedisConfig {

    /**
     * 設(shè)置 redisTemplate 的序列化設(shè)置
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 1.創(chuàng)建 redisTemplate 模版
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 2.關(guān)聯(lián) redisConnectionFactory
        template.setConnectionFactory(redisConnectionFactory);
        // 3.創(chuàng)建 序列化類
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 4.設(shè)置可見度
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 5.啟動默認的類型
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        // 6.序列化類,對象映射設(shè)置
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 7.設(shè)置 value 的轉(zhuǎn)化格式和 key 的轉(zhuǎn)化格式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

RedisAutoConfiguration上還有一下兩個注解,作用是從配置文件讀取redis相關(guān)的信息,ip、端口、密碼等

@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

 

2. 補充擴展(解釋為什么引用的包都報紅錯了,項目還能啟動)

所有的@Condition注解(包括衍生的)其實都對應(yīng)一個具體的實現(xiàn),這個實現(xiàn)類里面有個判斷方法叫做matches,返回的是個布爾類型判斷值。

打開ConditionalOnClass源碼如下,其Conditional注解傳遞的是個OnClassCondition.class,這就其對應(yīng)的判斷類,也就是說,當我們使用ConditionalOnClass注解時,其實際上調(diào)用的是OnClassCondition來判斷的

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {

	/**
	 * The classes that must be present. Since this annotation is parsed by loading class
	 * bytecode, it is safe to specify classes here that may ultimately not be on the
	 * classpath, only if this annotation is directly on the affected component and
	 * <b>not</b> if this annotation is used as a composed, meta-annotation. In order to
	 * use this annotation as a meta-annotation, only use the {@link #name} attribute.
	 * @return the classes that must be present
	 */
	Class<?>[] value() default {};

	/**
	 * The classes names that must be present.
	 * @return the class names that must be present.
	 */
	String[] name() default {};

}

ConditionalOnClass類圖如下,它繼承了condition接口

Springboot自動加載配置的原理解析

打開Condition接口如下,查看注釋,注釋中有說明 **條件判斷是在bean定義即將注冊到容器之前進行的,**看過springIoC源碼的同學(xué)應(yīng)該知道,spring創(chuàng)建一個對象的過程是當服務(wù)啟動后,先讀取xml配置文件(或者通過注解),根據(jù)配置文件先定義一個BeanDefinition,然后把這個bean給放到容器(在spring中實際就是一個Map),然后在根據(jù)bean定義,通過反射創(chuàng)建真正的對象。反射會觸發(fā)類加載,當condition條件不滿足時,根據(jù)如下注釋可知,bean定義后續(xù)都被攔截了,連注冊都不行,所以自然就不可能通過反射創(chuàng)建對象,不反射自然不會觸發(fā)類加載,不觸發(fā)類加載那么RedisAutoConfiguration當然啊不會加載,它不加載,那么即使它里面引用了一個不存在的類也不會有啥問題。

Springboot自動加載配置的原理解析

上面說的很繞,表達的不是很好,要想看懂以上部分需要掌握兩方面的知識:

  • 類加載原理,推薦看周志明老師的《深入理解JVM虛擬機》
  • spring IoC容器創(chuàng)建bean的原理,推薦《spring揭秘》,詳細看看IoC部分

 

3、又一個問題

spring-boot-autoconfigure.jar這個包中的RedisAutoConfiguration都報紅色錯誤了,那么spring官方是怎么打包出來spring-boot-autoconfigure.jar的??怎么給我們提供了一個報錯的包呢

//TODO

 

總結(jié)

到此這篇關(guān)于Springboot自動加載配置原理的文章就介紹到這了,更多相關(guān)Springboot自動加載配置原理內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/chen462488588/article/details/120769282

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产乱码一区二区三区 | 欧美激情视频一区二区三区在线播放 | 91资源在线视频 | 九九热这里只有精品8 | 免费一级片视频 | 久久久91精品国产一区二区三区 | 中文字幕第9页 | 欧美激情精品久久久久久黑人 | 欧美一级毛片日韩一级 | 一区二区高清 | 国产成人精品电影 | 亚洲视频 欧美视频 | 国产精品日韩一区二区 | 亚洲视频精品在线 | 欧美视频二区 | 久久久91精品国产一区二区三区 | 精品美女在线观看 | 超碰人人爱| 亚洲精品乱码久久久久久蜜糖图片 | 久久综合伊人 | 日本一区二区在线视频 | 自拍偷拍av | 中文在线一区二区 | 国产特级毛片aaaaaa高清 | 日韩在线观看一区 | 中文字幕在线观看一区二区 | 亚洲最新无码中文字幕久久 | 一级黄色片在线 | 亚洲成人精品 | 阿v视频在线 | 在线观看91视频 | 久久亚洲国产 | 精品少妇一区二区三区日产乱码 | 三级av在线 | 蜜桃成人 | 国产精品18久久久久久久久久久久 | 日本精品久久 | 成人精品网站在线观看 | av在线日韩 | 欧美午夜一区二区 | 亚洲一区二区三区四区五区中文 |