欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談springboot自動裝配原理

 更新時間:2021年05月14日 14:27:55   作者:向天再借500年  
作為Spring Boot的精髓,自動配置原理首當(dāng)其沖.今天就帶大家了解一下springboot自動裝配的原理,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下

一、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)編譯時,注解將不會出現(xiàn)在class源文件中

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

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

@Documented

java doc 會生成注解信息

@Inherited

是否會被繼承,就是如果一個子類繼承了使用了該注解的類,那么子類也能繼承該注解

@SpringBootConfiguration

標(biāo)注在某個類上,表示這是一個Spring Boot的配置類,本質(zhì)上也是使用了@Configuration注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

@EnableAutoConfiguration

@EnableAutoConfiguration告訴SpringBoot開啟自動配置,會幫我們自動去加載 自動配置類

@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啟動時,會自動加載這些配置類,初始化Bean

并不是所有Bean都會被初始化,在配置類中使用Condition來加載滿足條件的Bean

二、案例

自定義redis-starter,要求當(dāng)導(dǎo)入redis坐標(biāo)時,spirngboot自動創(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.首先新建兩個模塊

在這里插入圖片描述
在這里插入圖片描述

刪除一些沒有用的東西,和啟動類否則會報錯

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)前配置類所有在的包

排除的類型

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ù)切面表達式設(shè)置排除
REGEX 根據(jù)正則表達式設(shè)置排除
CUSTOM 自定義設(shè)置排除

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

按照自定義的方式來排除需要指定一個類,要實現(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對外提供的擴展類, 可以供我們?nèi)グ凑瘴覀兊姆绞竭M行排除

AutoConfigurationExcludeFilter :排除所有配置類并且是自動配置類中里面的其中一個
示例

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新提供的注解,它的作用是按照一定的條件進行判斷,滿足條件給容器注冊bean,實現(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)屬性和值才實例化Bean
@ConditionalOnClass 檢查類在加載器中是否存在對應(yīng)的類,如果有則被注解修飾的類就有資格被 Spring 容器所注冊,否則會被跳過。
@ConditionalOnBean 僅僅在當(dāng)前上下文中存在某個對象時,才會實例化一個 Bean
@ConditionalOnClass 某個 CLASS 位于類路徑上,才會實例化一個 Bean
@ConditionalOnExpression 當(dāng)表達式為 true 的時候,才會實例化一個 Bean
@ConditionalOnMissingBean 僅僅在當(dāng)前上下文中不存在某個對象時,才會實例化一個 Bean
@ConditionalOnMissingClass 某個 CLASS 類路徑上不存在的時候,才會實例化一個 Bean

案例

在springIOC容器中有一個User的bean,現(xiàn)要求:
引入jedis坐標(biāo)后,加載該bean,沒導(dǎo)入則不加載

實體類

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

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

啟動類

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)時,就可以打印user對象,當(dāng)刪除jedis的坐標(biāo)時,運行就會報錯 No bean named ‘user' available

四、案例升級

將類的判斷定義為動態(tài)的,判斷那個字節(jié)碼文件可以動態(tài)指定

自定義一個注解

添加上元注解

package com.blb.springbootyuanli.condition;

import org.springframework.context.annotation.Conditional;

import java.lang.annotation.*;

@Target({ElementType.TYPE, ElementType.METHOD}) //該注解的添加范圍
@Retention(RetentionPolicy.RUNTIME) //該注解的生效時機
@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.定義條件類:自定義類實現(xiàn)Condition接口,重寫重寫matches方法,在matches方法中進行邏輯判斷,返回boolean值

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

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

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

相關(guān)文章

  • java中的按位與(&)用法說明

    java中的按位與(&)用法說明

    這篇文章主要介紹了java中的按位與(&)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 對SpringMVC的@RequestParam的解釋

    對SpringMVC的@RequestParam的解釋

    下面小編就為大家?guī)硪黄獙pringMVC的@RequestParam的解釋。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 如何利用MyBatisX插件自動生成代碼

    如何利用MyBatisX插件自動生成代碼

    這篇文章主要介紹了如何利用MyBatisX插件自動生成代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • SpringBoot應(yīng)用部署于外置Tomcat容器的方法

    SpringBoot應(yīng)用部署于外置Tomcat容器的方法

    這篇文章主要介紹了SpringBoot應(yīng)用部署于外置Tomcat容器的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • spark中使用groupByKey進行分組排序的示例代碼

    spark中使用groupByKey進行分組排序的示例代碼

    這篇文章主要介紹了spark中使用groupByKey進行分組排序的實例代碼,本文通過實例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • java8中的默認(rèn)垃圾回收器(GC)

    java8中的默認(rèn)垃圾回收器(GC)

    這篇文章主要介紹了java8中的默認(rèn)垃圾回收器(GC),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringBoot中動態(tài)更新@Value配置方式

    SpringBoot中動態(tài)更新@Value配置方式

    這篇文章主要介紹了SpringBoot中動態(tài)更新@Value配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Maven 的配置文件路徑讀取方法

    Maven 的配置文件路徑讀取方法

    這篇文章主要介紹了Maven 的配置文件路徑讀取方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java常用面板之JScrollPane滾動面板實例詳解

    Java常用面板之JScrollPane滾動面板實例詳解

    這篇文章主要介紹了Java常用面板JScrollPane的簡單介紹和一個相關(guān)實例,,需要的朋友可以參考下。
    2017-08-08
  • hutool實戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)

    hutool實戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)

    這篇文章主要介紹了Go語言的io.ioutil標(biāo)準(zhǔn)庫使用,是Golang入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,如果能給你帶來幫助,請多多關(guān)注腳本之家的其他內(nèi)容
    2021-06-06

最新評論