淺談springboot自動(dòng)裝配原理
一、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 {
@Target(ElementType.TYPE)
設(shè)置當(dāng)前注解可以標(biāo)記在哪里,而SpringBootApplication只能用在類(lèi)上面
還有一些其他的設(shè)置
public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Formal parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE, /** * Type parameter declaration * * @since 1.8 */ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ TYPE_USE, /** * Module declaration. * * @since 9 */ MODULE }
@Retention(RetentionPolicy.RUNTIME)
public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. */ CLASS, /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement */ RUNTIME }
SOURCE 當(dāng)編譯時(shí),注解將不會(huì)出現(xiàn)在class源文件中
CLASS 注解將會(huì)保留在class源文件中,但是不會(huì)被jvm加載,也就意味著不能通過(guò)反射去找到該注解,因?yàn)闆](méi)有加載到j(luò)ava虛擬機(jī)中
RUNTIME是既會(huì)保留在源文件中,也會(huì)被虛擬機(jī)加載
@Documented
java doc 會(huì)生成注解信息
@Inherited
是否會(huì)被繼承,就是如果一個(gè)子類(lèi)繼承了使用了該注解的類(lèi),那么子類(lèi)也能繼承該注解
@SpringBootConfiguration
標(biāo)注在某個(gè)類(lèi)上,表示這是一個(gè)Spring Boot的配置類(lèi),本質(zhì)上也是使用了@Configuration注解
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {
@EnableAutoConfiguration
@EnableAutoConfiguration告訴SpringBoot開(kāi)啟自動(dòng)配置,會(huì)幫我們自動(dòng)去加載 自動(dòng)配置類(lèi)
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
@AutoConfigurationPackage
將當(dāng)前配置類(lèi)所在包保存在BasePackages的Bean中。供Spring內(nèi)部使用
@Import({AutoConfigurationImportSelector.class})來(lái)加載配置類(lèi)
配置文件的位置:META-INF/spring.factories,該配置文件中定義了大量的配置類(lèi),當(dāng)springboot啟動(dòng)時(shí),會(huì)自動(dòng)加載這些配置類(lèi),初始化Bean
并不是所有Bean都會(huì)被初始化,在配置類(lèi)中使用Condition來(lái)加載滿(mǎn)足條件的Bean
二、案例
自定義redis-starter,要求當(dāng)導(dǎo)入redis坐標(biāo)時(shí),spirngboot自動(dòng)創(chuàng)建jedis的Bean
步驟
1.創(chuàng)建redis-spring-boot-autoconfigure模塊
2.創(chuàng)建redis-spring-boot-starter模塊,依賴(lài)redis-spring-boot-autoconfigure的模塊
3.在redis-spring-boot-autoconfigure模塊中初始化jedis的bean,并定義META-INF/spring.factories文件
4.在測(cè)試模塊中引入自定義的redis-starter依賴(lài),測(cè)試獲取jedis的bean,操作redis
1.首先新建兩個(gè)模塊
刪除一些沒(méi)有用的東西,和啟動(dòng)類(lèi)否則會(huì)報(bào)錯(cuò)
2.redis-spring-boot-starter模塊的pom.xml里面引入redis-spring-boot-autoconfigure的模塊的坐標(biāo)
3.RedisAutoConfiguration配置類(lèi)
package com.blb; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; @Configuration @EnableConfigurationProperties(RedisProperties.class) public class RedisAutoConfiguration { // 提供Jedis的bean @Bean public Jedis jedis(RedisProperties redisProperties){ return new Jedis(redisProperties.getHost(),redisProperties.getPort()); } }
RedisProperties
package com.blb; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "redis") public class RedisProperties { private String host="localhost"; private int port=6379; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
@ComponentScan
掃描包 相當(dāng)于在spring.xml 配置中context:comonent-scan 但是并沒(méi)有指定basepackage,如果沒(méi)有指定spring底層會(huì)自動(dòng)掃描當(dāng)前配置類(lèi)所有在的包
排除的類(lèi)型
public enum FilterType { /** * Filter candidates marked with a given annotation. * @see org.springframework.core.type.filter.AnnotationTypeFilter */ ANNOTATION, /** * Filter candidates assignable to a given type. * @see org.springframework.core.type.filter.AssignableTypeFilter */ ASSIGNABLE_TYPE, /** * Filter candidates matching a given AspectJ type pattern expression. * @see org.springframework.core.type.filter.AspectJTypeFilter */ ASPECTJ, /** * Filter candidates matching a given regex pattern. * @see org.springframework.core.type.filter.RegexPatternTypeFilter */ REGEX, /** Filter candidates using a given custom * {@link org.springframework.core.type.filter.TypeFilter} implementation. */ CUSTOM }
ANNOTATION 默認(rèn)根據(jù)注解的完整限定名設(shè)置排除
ASSIGNABLE_TYPE 根據(jù)類(lèi)的完整限定名排除
ASPECTJ 根據(jù)切面表達(dá)式設(shè)置排除
REGEX 根據(jù)正則表達(dá)式設(shè)置排除
CUSTOM 自定義設(shè)置排除
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
按照自定義的方式來(lái)排除需要指定一個(gè)類(lèi),要實(shí)現(xiàn)TypeFilter接口,重寫(xiě)match方法
public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { if (this.beanFactory instanceof ListableBeanFactory && this.getClass() == TypeExcludeFilter.class) { Iterator var3 = this.getDelegates().iterator(); while(var3.hasNext()) { TypeExcludeFilter delegate = (TypeExcludeFilter)var3.next(); if (delegate.match(metadataReader, metadataReaderFactory)) { return true; } } } return false; } }
TypeExcludeFilter :springboot對(duì)外提供的擴(kuò)展類(lèi), 可以供我們?nèi)グ凑瘴覀兊姆绞竭M(jìn)行排除
AutoConfigurationExcludeFilter :排除所有配置類(lèi)并且是自動(dòng)配置類(lèi)中里面的其中一個(gè)
示例
package com.blb.springbootyuanli.config; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import java.io.IOException; public class MyTypeExcludeFilter extends TypeExcludeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { if(metadataReader.getClassMetadata().getClass()==UserConfig.class){ return true; } return false; } }
三、Condition
@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿(mǎn)足條件給容器注冊(cè)bean,實(shí)現(xiàn)選擇性的創(chuàng)建bean的操作,該注解為條件裝配注解
源碼
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { /** * All {@link Condition} classes that must {@linkplain Condition#matches match} * in order for the component to be registered. */ Class<? extends Condition>[] value(); }
@FunctionalInterface public interface Condition { /** * Determine if the condition matches. * @param context the condition context * @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class} * or {@link org.springframework.core.type.MethodMetadata method} being checked * @return {@code true} if the condition matches and the component can be registered, * or {@code false} to veto the annotated component's registration */ boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata); }
重寫(xiě)matches方法如果返回true spring則會(huì)幫你創(chuàng)建該對(duì)象,否則則不會(huì)
springboot提供的常用條件注解
@ConditionalOnProperty:判斷文件中是否有對(duì)應(yīng)屬性和值才實(shí)例化Bean @ConditionalOnClass 檢查類(lèi)在加載器中是否存在對(duì)應(yīng)的類(lèi),如果有則被注解修飾的類(lèi)就有資格被 Spring 容器所注冊(cè),否則會(huì)被跳過(guò)。 @ConditionalOnBean 僅僅在當(dāng)前上下文中存在某個(gè)對(duì)象時(shí),才會(huì)實(shí)例化一個(gè) Bean @ConditionalOnClass 某個(gè) CLASS 位于類(lèi)路徑上,才會(huì)實(shí)例化一個(gè) Bean @ConditionalOnExpression 當(dāng)表達(dá)式為 true 的時(shí)候,才會(huì)實(shí)例化一個(gè) Bean @ConditionalOnMissingBean 僅僅在當(dāng)前上下文中不存在某個(gè)對(duì)象時(shí),才會(huì)實(shí)例化一個(gè) Bean @ConditionalOnMissingClass 某個(gè) CLASS 類(lèi)路徑上不存在的時(shí)候,才會(huì)實(shí)例化一個(gè) Bean
案例
在springIOC容器中有一個(gè)User的bean,現(xiàn)要求:
引入jedis坐標(biāo)后,加載該bean,沒(méi)導(dǎo)入則不加載
實(shí)體類(lèi)
package com.blb.springbootyuanli.entity; public class User { private String name; private int age; get/set
UserConfig
配置類(lèi)
package com.blb.springbootyuanli.config; import com.blb.springbootyuanli.condition.UserCondition; import com.blb.springbootyuanli.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @Configuration public class UserConfig { @Bean @Conditional(UserCondition.class) public User user(){ return new User(); } }
UserCondition
實(shí)現(xiàn)Condition接口,重寫(xiě)matches方法
package com.blb.springbootyuanli.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class UserCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //思路判斷jedis的class文件是否存在 boolean flag=true; try { Class<?> aClass = Class.forName("redis.clients.jedis.Jedis"); } catch (ClassNotFoundException e) { flag=false; } return flag; } }
啟動(dòng)類(lèi)
package com.blb.springbootyuanli; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringbootYuanliApplication { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(SpringbootYuanliApplication.class, args); Object user = app.getBean("user"); System.out.println(user); } }
當(dāng)我們?cè)趐om.xml引入jedis的坐標(biāo)時(shí),就可以打印user對(duì)象,當(dāng)刪除jedis的坐標(biāo)時(shí),運(yùn)行就會(huì)報(bào)錯(cuò) No bean named ‘user' available
四、案例升級(jí)
將類(lèi)的判斷定義為動(dòng)態(tài)的,判斷那個(gè)字節(jié)碼文件可以動(dòng)態(tài)指定
自定義一個(gè)注解
添加上元注解
package com.blb.springbootyuanli.condition; import org.springframework.context.annotation.Conditional; import java.lang.annotation.*; @Target({ElementType.TYPE, ElementType.METHOD}) //該注解的添加范圍 @Retention(RetentionPolicy.RUNTIME) //該注解的生效時(shí)機(jī) @Documented //生成javadoc的文檔 @Conditional(UserCondition.class) public @interface UserClassCondition { String[] value(); }
UserConfig
package com.blb.springbootyuanli.config; import com.blb.springbootyuanli.condition.UserClassCondition; import com.blb.springbootyuanli.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UserConfig { @Bean //@Conditional(UserCondition.class) @UserClassCondition("redis.clients.jedis.Jedis") public User user(){ return new User(); } }
package com.blb.springbootyuanli.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; import java.util.Map; public class UserCondition implements Condition { /** * * @param context 上下文對(duì)象,用于獲取環(huán)境,ioc容器,classloader對(duì)象 * @param metadata 注解元對(duì)象??梢垣@取注解定義的屬性值 * @return */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //思路判斷指定屬性的class文件是否存在 //獲取注解屬性值 value Map<String,Object> map=metadata.getAnnotationAttributes(UserClassCondition.class.getName()); String[] values= (String[])map.get("value"); boolean flag=true; try { for(String classname:values){ Class<?> aClass = Class.forName(classname); } } catch (ClassNotFoundException e) { flag=false; } return flag; } }
測(cè)試自帶的注解
package com.blb.springbootyuanli.config; import com.blb.springbootyuanli.entity.User; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UserConfig { @Bean //@Conditional(UserCondition.class) //@UserClassCondition("redis.clients.jedis.Jedis") @ConditionalOnProperty(name="age",havingValue = "18") //只有在配置文件中有age并且值為18spring在能注冊(cè)該bean public User user(){ return new User(); } }
五、小結(jié)
自定義條件:
1.定義條件類(lèi):自定義類(lèi)實(shí)現(xiàn)Condition接口,重寫(xiě)重寫(xiě)matches方法,在matches方法中進(jìn)行邏輯判斷,返回boolean值
2.matches方法的兩個(gè)參數(shù):
context:上下文對(duì)象,可以獲取屬性值,獲取類(lèi)加載器,獲取BeanFactory
metadata:元數(shù)據(jù)對(duì)象,用于獲取注解屬性
3.判斷條件:在初始化Bean時(shí),使用@Conditional(條件類(lèi).class) 注解
到此這篇關(guān)于淺談springboot自動(dòng)裝配原理的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 深入淺析SpringBoot中的自動(dòng)裝配
- SpringBoot啟動(dòng)及自動(dòng)裝配原理過(guò)程詳解
- springboot?無(wú)法自動(dòng)裝配的問(wèn)題
- SpringBoot(cloud)自動(dòng)裝配bean找不到類(lèi)型的問(wèn)題
- SpringBoot自動(dòng)裝配原理詳解
- Springboot自動(dòng)裝配實(shí)現(xiàn)過(guò)程代碼實(shí)例
- 淺析SpringBoot自動(dòng)裝配的實(shí)現(xiàn)
- Java Springboot自動(dòng)裝配原理詳解
- java中SpringBoot?自動(dòng)裝配的原理分析
相關(guān)文章
對(duì)SpringMVC的@RequestParam的解釋
下面小編就為大家?guī)?lái)一篇對(duì)SpringMVC的@RequestParam的解釋。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09SpringBoot應(yīng)用部署于外置Tomcat容器的方法
這篇文章主要介紹了SpringBoot應(yīng)用部署于外置Tomcat容器的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06spark中使用groupByKey進(jìn)行分組排序的示例代碼
這篇文章主要介紹了spark中使用groupByKey進(jìn)行分組排序的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03SpringBoot中動(dòng)態(tài)更新@Value配置方式
這篇文章主要介紹了SpringBoot中動(dòng)態(tài)更新@Value配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Java常用面板之JScrollPane滾動(dòng)面板實(shí)例詳解
這篇文章主要介紹了Java常用面板JScrollPane的簡(jiǎn)單介紹和一個(gè)相關(guān)實(shí)例,,需要的朋友可以參考下。2017-08-08hutool實(shí)戰(zhàn):IoUtil 流操作工具類(lèi)(將內(nèi)容寫(xiě)到流中)
這篇文章主要介紹了Go語(yǔ)言的io.ioutil標(biāo)準(zhǔn)庫(kù)使用,是Golang入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下,如果能給你帶來(lái)幫助,請(qǐng)多多關(guān)注腳本之家的其他內(nèi)容2021-06-06