圖文并茂,Spring Boot Starter 萬(wàn)字詳解!還有誰(shuí)不會(huì)?
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

-? ? ?一、SpringBoot的starter簡(jiǎn)介? ? -
1.1 什么是starter(場(chǎng)景啟動(dòng)器)

-? ? ?二、SpringBoot場(chǎng)景啟動(dòng)器的原理??? -
2.1 自動(dòng)配置原理
2.1.1 自動(dòng)配置類的獲取與注入
@SpringBootApplication?//標(biāo)注這個(gè)類是一個(gè)springboot的應(yīng)用
public?class?CommunityApplication?{
????public?static?void?main(String[]?args)?{
????????//將springboot應(yīng)用啟動(dòng)
????????SpringApplication.run(CommunityApplication.class,?args);
????}
}
2.1.2 自動(dòng)配置的過(guò)程
//表示這是一個(gè)配置類,和以前編寫(xiě)的配置文件一樣,也可以給容器中添加組件;
@Configuration
//將與配置文件綁定好的某個(gè)類注入到容器中,使其生效
//進(jìn)入這個(gè)HttpProperties查看,將配置文件中對(duì)應(yīng)的值和HttpProperties綁定起來(lái);
//并把HttpProperties加入到ioc容器中
@EnableConfigurationProperties(HttpProperties.class)
//Spring底層@Conditional注解
//根據(jù)不同的條件判斷,如果滿足指定的條件,整個(gè)配置類里面的配置就會(huì)生效;
//這里的意思就是判斷當(dāng)前應(yīng)用是否是web應(yīng)用,如果是,當(dāng)前配置類生效
@ConditionalOnWebApplication(type?=?ConditionalOnWebApplication.Type.SERVLET)
//判斷系統(tǒng)中有沒(méi)有CharacterEncodingFilter這個(gè)類,如果有配置類才生效
@ConditionalOnClass(CharacterEncodingFilter.class)
//判斷配置文件中是否存在某個(gè)配置:spring.http.encoding.enabled;
//matchIfMissing?=?true表明即使我們配置文件中不配置pring.http.encoding.enabled=true,該配置類也是默認(rèn)生效的;
@ConditionalOnProperty(prefix?=?"spring.http.encoding",?value?=?"enabled",?matchIfMissing?=?true)
public?class?HttpEncodingAutoConfiguration?{
??//該類已經(jīng)與配置文件綁定了
?private?final?HttpProperties.Encoding?properties;
??//構(gòu)建該自動(dòng)配置類時(shí)將與配置文件綁定的配置類作為入?yún)鬟f進(jìn)去
?public?HttpEncodingAutoConfiguration(HttpProperties?properties)?{
??this.properties?=?properties.getEncoding();
?}
?@Bean
?@ConditionalOnMissingBean
?public?CharacterEncodingFilter?characterEncodingFilter()?{
??CharacterEncodingFilter?filter?=?new?OrderedCharacterEncodingFilter();
??filter.setEncoding(this.properties.getCharset().name());?//注入bean時(shí)使用配置類中屬性的值進(jìn)行初始化,相當(dāng)于將配置文件中的值映射到了組件的某些屬性上
??filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
??filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
??return?filter;?//注入配置好的bean
?}
}
//從配置文件中獲取指定的值和bean的屬性進(jìn)行綁定
@ConfigurationProperties(prefix?=?"spring.http")?
public?class?HttpProperties?{
????//?.....
}
2.2 SpringBoot自動(dòng)配置使用總結(jié)
| @Conditional擴(kuò)展注解 | 作用(判斷是否滿足當(dāng)前條件) |
|---|---|
| @ConditionalOnNotWebApplication | 當(dāng)前不是web環(huán)境 |
| @ConditionalOnWebApplication | 當(dāng)前是web環(huán)境 |
| @ConditionalOnResource | 類路徑下是否有指定資源文件 |
| @ConditionalOnExpression | 滿足SpEL表達(dá)式指定 |
| @ConditionalOnMissingBean | 容器中不存在指定的Bean |
| @ConditionalOnBean | 容器中是否存在指定的Bean |
| @ConditionalOnSingleCandidate | 容器中只有一個(gè)指定的Bean,或者這個(gè)Bean是首選Bean |
| @ConditionalOnJava | 系統(tǒng)的java版本是否符合要求 |
| @ConditionalOnMissingClass | 系統(tǒng)中沒(méi)有指定的類 |
| @ConditionalOnClass | 系統(tǒng)中有指定的類 |
| @ConditionalOnProperty | 系統(tǒng)中指定的屬性是否有指定的值 |
#在配置文件中開(kāi)啟springboot的調(diào)試類
debug=true
Positive?matches:
-----------------
???AopAutoConfiguration?matched:
??????-?@ConditionalOnClass?found?required?classes?'org.springframework.context.annotation.EnableAspectJAutoProxy',?'org.aspectj.lang.annotation.Aspect',?'org.aspectj.lang.reflect.Advice',?'org.aspectj.weaver.AnnotatedElement'?(OnClassCondition)
??????-?@ConditionalOnProperty?(spring.aop.auto=true)?matched?(OnPropertyCondition)
???AopAutoConfiguration.CglibAutoProxyConfiguration?matched:
??????-?@ConditionalOnProperty?(spring.aop.proxy-target-class=true)?matched?(OnPropertyCondition)
???AuditAutoConfiguration#auditListener?matched:
??????-?@ConditionalOnMissingBean?(types:?org.springframework.boot.actuate.audit.listener.AbstractAuditListener;?SearchStrategy:?all)?did?not?find?any?beans?(OnBeanCondition)
???AuditAutoConfiguration#authenticationAuditListener?matched:
??????-?@ConditionalOnClass?found?required?class?'org.springframework.security.authentication.event.AbstractAuthenticationEvent'?(OnClassCondition)
??????-?@ConditionalOnMissingBean?(types:?org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener;?SearchStrategy:?all)?did?not?find?any?beans?(OnBeanCondition)
Negative?matches:
-----------------
???ActiveMQAutoConfiguration:
??????Did?not?match:
?????????-?@ConditionalOnClass?did?not?find?required?class?'javax.jms.ConnectionFactory'?(OnClassCondition)
???AopAutoConfiguration.JdkDynamicAutoProxyConfiguration:
??????Did?not?match:
?????????-?@ConditionalOnProperty?(spring.aop.proxy-target-class=false)?did?not?find?property?'proxy-target-class'?(OnPropertyCondition)
???AppOpticsMetricsExportAutoConfiguration:
??????Did?not?match:
?????????-?@ConditionalOnClass?did?not?find?required?class?'io.micrometer.appoptics.AppOpticsMeterRegistry'?(OnClassCondition)
???ArtemisAutoConfiguration:
??????Did?not?match:
?????????-?@ConditionalOnClass?did?not?find?required?class?'javax.jms.ConnectionFactory'?(OnClassCondition)
Exclusions:
-----------
????org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Unconditional?classes:
----------------------
????org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
????org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration
????org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration
????org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration

-? ? ?三、自定義場(chǎng)景啟動(dòng)器??? -
3.1 starter的命名規(guī)范
3.2 starter模塊整體結(jié)構(gòu)
3.3 autoconfigure模塊開(kāi)發(fā)
3.3.1 依賴引入
?
????????
????????????org.springframework.boot
????????????spring-boot-autoconfigure ?//包含很多與自動(dòng)配置相關(guān)的注解的定義,必須要引入
????????
????????
????????????org.springframework.boot
????????????spring-boot-configuration-processor ?//非必須的,引入后可以在配置文件中輸入我們自定義配置的時(shí)候有相應(yīng)的提示,也可以通過(guò)其他.properties文件為相關(guān)類進(jìn)行屬性映射(SpringBoot默認(rèn)使用application.yml)
????????????true
????????
????
3.3.2 xxxAutoConfiguration的實(shí)現(xiàn)
@Configuration?
@ConditionalOnxxx
@ConditionalOnxxx//限定自動(dòng)配置類生效的一些條件
@EnableConfigurationProperties(xxxProperties.class)
public?class?xxxAutoConfiguration?{
?@Autowired
?private?xxxProperties?properties;
????@Bean
????public?static?BeanYouNeed?beanYouNeed()?{
?????BeanYouNeed?bean?=?new?BeanYouNeed()
?????bean.setField(properties.get(field));
?????bean.setField(properties.get(field));
?????bean.setField(properties.get(field));
????????......
????}
}
3.3.3 xxxProperties的實(shí)現(xiàn)
@ConfigurationProperties(prefix?=?"your?properties")?//使用@ConfigurationProperties注解綁定配置文件
public?class?xxxProperties?{
????private?boolean?enabled?=?true;
????private?String?clientId;
????private?String?beanName;
????private?String?scanBasePackage;
????private?String?path;
????private?String?token;
}
3.3.4 配置spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.meituan.xframe.boot.mcc.autoconfigure.xxxAutoConfiguration
3.4 Starter模塊開(kāi)發(fā)
?
????================================================================
????添加了對(duì)autoconfigure模塊的引用
????????
????????????com.test.starter
????????????xxx-spring-boot-autoconfigure
????????
????===============================================================
????其他的一些必要依賴項(xiàng)
????????
????????????commons-collections
????????????commons-collections
????????
????
? 作者?|??威尼斯的淚H
來(lái)源 |??csdn.net/qq_21310939/article/details/107401400
加鋒哥微信:?java1239?? 圍觀鋒哥朋友圈,每天推送Java干貨!
評(píng)論
圖片
表情







