国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

淺談springboot自動(dòng)裝配原理

發(fā)布時(shí)間:2021-07-06 11:13 來(lái)源:腳本之家 閱讀:0 作者:向天再借500年 欄目: 開(kāi)發(fā)技術(shù)

目錄

一、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)

設置當前注解可以標記在哪里,而SpringBootApplication只能用在類(lèi)上面
還有一些其他的設置

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 當編譯時(shí),注解將不會(huì )出現在class源文件中

CLASS 注解將會(huì )保留在class源文件中,但是不會(huì )被jvm加載,也就意味著(zhù)不能通過(guò)反射去找到該注解,因為沒(méi)有加載到j(luò )ava虛擬機中

RUNTIME是既會(huì )保留在源文件中,也會(huì )被虛擬機加載

@Documented

java doc 會(huì )生成注解信息

@Inherited

是否會(huì )被繼承,就是如果一個(gè)子類(lèi)繼承了使用了該注解的類(lèi),那么子類(lèi)也能繼承該注解

@SpringBootConfiguration

標注在某個(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

將當前配置類(lèi)所在包保存在BasePackages的Bean中。供Spring內部使用

@Import({AutoConfigurationImportSelector.class})來(lái)加載配置類(lèi)

配置文件的位置:META-INF/spring.factories,該配置文件中定義了大量的配置類(lèi),當springboot啟動(dòng)時(shí),會(huì )自動(dòng)加載這些配置類(lèi),初始化Bean

并不是所有Bean都會(huì )被初始化,在配置類(lèi)中使用Condition來(lái)加載滿(mǎn)足條件的Bean

二、案例

自定義redis-starter,要求當導入redis坐標時(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.在測試模塊中引入自定義的redis-starter依賴(lài),測試獲取jedis的bean,操作redis

1.首先新建兩個(gè)模塊


刪除一些沒(méi)有用的東西,和啟動(dòng)類(lèi)否則會(huì )報錯

2.redis-spring-boot-starter模塊的pom.xml里面引入redis-spring-boot-autoconfigure的模塊的坐標

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

掃描包 相當于在spring.xml 配置中context:comonent-scan 但是并沒(méi)有指定basepackage,如果沒(méi)有指定spring底層會(huì )自動(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 默認根據注解的完整限定名設置排除
ASSIGNABLE_TYPE 根據類(lèi)的完整限定名排除
ASPECTJ 根據切面表達式設置排除
REGEX 根據正則表達式設置排除
CUSTOM 自定義設置排除

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

按照自定義的方式來(lái)排除需要指定一個(gè)類(lèi),要實(shí)現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對外提供的擴展類(lèi), 可以供我們去按照我們的方式進(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)足條件給容器注冊bean,實(shí)現選擇性的創(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 )建該對象,否則則不會(huì )

springboot提供的常用條件注解

@ConditionalOnProperty:判斷文件中是否有對應屬性和值才實(shí)例化Bean
@ConditionalOnClass 檢查類(lèi)在加載器中是否存在對應的類(lèi),如果有則被注解修飾的類(lèi)就有資格被 Spring 容器所注冊,否則會(huì )被跳過(guò)。
@ConditionalOnBean 僅僅在當前上下文中存在某個(gè)對象時(shí),才會(huì )實(shí)例化一個(gè) Bean
@ConditionalOnClass 某個(gè) CLASS 位于類(lèi)路徑上,才會(huì )實(shí)例化一個(gè) Bean
@ConditionalOnExpression 當表達式為 true 的時(shí)候,才會(huì )實(shí)例化一個(gè) Bean
@ConditionalOnMissingBean 僅僅在當前上下文中不存在某個(gè)對象時(shí),才會(huì )實(shí)例化一個(gè) Bean
@ConditionalOnMissingClass 某個(gè) CLASS 類(lèi)路徑上不存在的時(shí)候,才會(huì )實(shí)例化一個(gè) Bean

案例

在springIOC容器中有一個(gè)User的bean,現要求:
引入jedis坐標后,加載該bean,沒(méi)導入則不加載

實(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í)現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);

    }

}

當我們在pom.xml引入jedis的坐標時(shí),就可以打印user對象,當刪除jedis的坐標時(shí),運行就會(huì )報錯 No bean named ‘user' available

四、案例升級

將類(lèi)的判斷定義為動(dòng)態(tài)的,判斷那個(gè)字節碼文件可以動(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í)機
@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();
    }
}

五、小結

自定義條件:

1.定義條件類(lèi):自定義類(lèi)實(shí)現Condition接口,重寫(xiě)重寫(xiě)matches方法,在matches方法中進(jìn)行邏輯判斷,返回boolean值

2.matches方法的兩個(gè)參數:
context:上下文對象,可以獲取屬性值,獲取類(lèi)加載器,獲取BeanFactory
metadata:元數據對象,用于獲取注解屬性

3.判斷條件:在初始化Bean時(shí),使用@Conditional(條件類(lèi).class) 注解

到此這篇關(guān)于淺談springboot自動(dòng)裝配原理的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

sp
青青久在线视频免费观看| 玩弄放荡人妻少妇系列视频| 日产AV基地无码内射| 野外做受又硬又粗又大视幕| 国产老女人卖婬| aaaaa级少妇高潮大片|