淺談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只能用在類上面
還有一些其他的設(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í),注解將不會出現(xiàn)在class源文件中
CLASS 注解將會保留在class源文件中,但是不會被jvm加載,也就意味著不能通過反射去找到該注解,因?yàn)闆]有加載到j(luò)ava虛擬機(jī)中
RUNTIME是既會保留在源文件中,也會被虛擬機(jī)加載
@Documented
java doc 會生成注解信息
@Inherited
是否會被繼承,就是如果一個(gè)子類繼承了使用了該注解的類,那么子類也能繼承該注解
@SpringBootConfiguration
標(biāo)注在某個(gè)類上,表示這是一個(gè)Spring Boot的配置類,本質(zhì)上也是使用了@Configuration注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@EnableAutoConfiguration
@EnableAutoConfiguration告訴SpringBoot開啟自動(dòng)配置,會幫我們自動(dòng)去加載 自動(dòng)配置類
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage
將當(dāng)前配置類所在包保存在BasePackages的Bean中。供Spring內(nèi)部使用
@Import({AutoConfigurationImportSelector.class})來加載配置類
配置文件的位置:META-INF/spring.factories,該配置文件中定義了大量的配置類,當(dāng)springboot啟動(dòng)時(shí),會自動(dòng)加載這些配置類,初始化Bean
并不是所有Bean都會被初始化,在配置類中使用Condition來加載滿足條件的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模塊,依賴redis-spring-boot-autoconfigure的模塊
3.在redis-spring-boot-autoconfigure模塊中初始化jedis的bean,并定義META-INF/spring.factories文件
4.在測試模塊中引入自定義的redis-starter依賴,測試獲取jedis的bean,操作redis
1.首先新建兩個(gè)模塊


刪除一些沒有用的東西,和啟動(dòng)類否則會報(bào)錯(cuò)
2.redis-spring-boot-starter模塊的pom.xml里面引入redis-spring-boot-autoconfigure的模塊的坐標(biāo)
3.RedisAutoConfiguration配置類
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 但是并沒有指定basepackage,如果沒有指定spring底層會自動(dòng)掃描當(dāng)前配置類所有在的包
排除的類型
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ù)類的完整限定名排除
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) })
按照自定義的方式來排除需要指定一個(gè)類,要實(shí)現(xiàn)TypeFilter接口,重寫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對外提供的擴(kuò)展類, 可以供我們?nèi)グ凑瘴覀兊姆绞竭M(jìn)行排除
AutoConfigurationExcludeFilter :排除所有配置類并且是自動(dòng)配置類中里面的其中一個(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)行判斷,滿足條件給容器注冊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);
}
重寫matches方法如果返回true spring則會幫你創(chuàng)建該對象,否則則不會
springboot提供的常用條件注解
@ConditionalOnProperty:判斷文件中是否有對應(yīng)屬性和值才實(shí)例化Bean @ConditionalOnClass 檢查類在加載器中是否存在對應(yīng)的類,如果有則被注解修飾的類就有資格被 Spring 容器所注冊,否則會被跳過。 @ConditionalOnBean 僅僅在當(dāng)前上下文中存在某個(gè)對象時(shí),才會實(shí)例化一個(gè) Bean @ConditionalOnClass 某個(gè) CLASS 位于類路徑上,才會實(shí)例化一個(gè) Bean @ConditionalOnExpression 當(dāng)表達(dá)式為 true 的時(shí)候,才會實(shí)例化一個(gè) Bean @ConditionalOnMissingBean 僅僅在當(dāng)前上下文中不存在某個(gè)對象時(shí),才會實(shí)例化一個(gè) Bean @ConditionalOnMissingClass 某個(gè) CLASS 類路徑上不存在的時(shí)候,才會實(shí)例化一個(gè) Bean
案例
在springIOC容器中有一個(gè)User的bean,現(xiàn)要求:
引入jedis坐標(biāo)后,加載該bean,沒導(dǎo)入則不加載
實(shí)體類
package com.blb.springbootyuanli.entity;
public class User {
private String name;
private int age;
get/set
UserConfig
配置類
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接口,重寫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)類
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)我們在pom.xml引入jedis的坐標(biāo)時(shí),就可以打印user對象,當(dāng)刪除jedis的坐標(biāo)時(shí),運(yùn)行就會報(bào)錯(cuò) No bean named ‘user' available
四、案例升級
將類的判斷定義為動(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 上下文對象,用于獲取環(huán)境,ioc容器,classloader對象
* @param metadata 注解元對象??梢垣@取注解定義的屬性值
* @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;
}
}
測試自帶的注解
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在能注冊該bean
public User user(){
return new User();
}
}
五、小結(jié)
自定義條件:
1.定義條件類:自定義類實(shí)現(xiàn)Condition接口,重寫重寫matches方法,在matches方法中進(jìn)行邏輯判斷,返回boolean值
2.matches方法的兩個(gè)參數(shù):
context:上下文對象,可以獲取屬性值,獲取類加載器,獲取BeanFactory
metadata:元數(shù)據(jù)對象,用于獲取注解屬性
3.判斷條件:在初始化Bean時(shí),使用@Conditional(條件類.class) 注解
到此這篇關(guān)于淺談springboot自動(dòng)裝配原理的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot應(yīng)用部署于外置Tomcat容器的方法
這篇文章主要介紹了SpringBoot應(yīng)用部署于外置Tomcat容器的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
spark中使用groupByKey進(jìn)行分組排序的示例代碼
這篇文章主要介紹了spark中使用groupByKey進(jìn)行分組排序的實(shí)例代碼,本文通過實(shí)例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
SpringBoot中動(dòng)態(tài)更新@Value配置方式
這篇文章主要介紹了SpringBoot中動(dòng)態(tài)更新@Value配置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java常用面板之JScrollPane滾動(dòng)面板實(shí)例詳解
這篇文章主要介紹了Java常用面板JScrollPane的簡單介紹和一個(gè)相關(guān)實(shí)例,,需要的朋友可以參考下。2017-08-08
hutool實(shí)戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)
這篇文章主要介紹了Go語言的io.ioutil標(biāo)準(zhǔn)庫使用,是Golang入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,如果能給你帶來幫助,請多多關(guān)注腳本之家的其他內(nèi)容2021-06-06

