最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

最新springboot中必須要了解的自動(dòng)裝配原理

 更新時(shí)間:2022年05月20日 11:30:20   作者:奔走的王木木Sir  
本文給大家介紹springboot中必須要了解的自動(dòng)裝配原理,spring-boot-dependencies:核心依賴都在父工程中,這個(gè)里面主要是管理項(xiàng)目的資源過濾及插件,本文對(duì)springboot自動(dòng)裝配原理給大家介紹的非常詳細(xì),需要的朋友參考下吧

1.pom.xml

父 依 賴 \textcolor{orange}{父依賴} 父依賴

spring-boot-dependencies:核心依賴都在父工程中

這里ctrl+左鍵,點(diǎn)擊之后我們可以看到父依賴

這個(gè)里面主要是管理項(xiàng)目的資源過濾及插件,我們發(fā)現(xiàn)他還有一個(gè)父依賴

看看下面這個(gè),熟悉嗎?

再點(diǎn)進(jìn)去,我們發(fā)現(xiàn)有很多的依賴。這就是SpringBoot的版本控制中心。

這個(gè)地方才是真正管理SpringBoot應(yīng)用里面所有依賴的地方,也就是版本控制中心。

我們?cè)趯懟蛞胍恍㏒pringBoot依賴的時(shí)候,不需要指定版本,就是因?yàn)橛羞@些版本倉庫。

2.啟動(dòng)器

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

啟動(dòng)器就是springboot的啟動(dòng)場(chǎng)景 \textcolor{red}{啟動(dòng)器就是SpringBoot的啟動(dòng)場(chǎng)景} 啟動(dòng)器就是SpringBoot的啟動(dòng)場(chǎng)景

springboot-boot-starter-xxx:就是spring-boot的場(chǎng)景啟動(dòng)器

spring-boot-starter-web:幫我們導(dǎo)入了web模塊正常運(yùn)行所依賴的組件;也就是自動(dòng)導(dǎo)入web環(huán)境所有的依賴

SpringBoot將所有的功能場(chǎng)景都抽取出來,做成一個(gè)個(gè)的starter (啟動(dòng)器);

要用什么功能就導(dǎo)入什么樣的場(chǎng)景啟動(dòng)器:只需要在項(xiàng)目中引入這些starter即可,所有相關(guān)的依賴都會(huì)導(dǎo)入進(jìn)來 ;

我們未來也可以自己自定義 starter;

3.主程序

//程序的主入口
//@SpringBootApplication:標(biāo)注這個(gè)類是一個(gè)springBoot的應(yīng)用
@SpringBootApplication
public class HelloSpringBootApplication {
   public static void main(String[] args) {
      //將springBoot應(yīng)用啟動(dòng)
      SpringApplication.run(HelloSpringBootApplication.class, args);
   }
}

看著如此的簡(jiǎn)單,它就是通過反射加載這個(gè)類的對(duì)象,這是表面意思,我們看不到它為啥啟動(dòng)。

首先我們來看

3.1注解

@SpringBootApplication

我們點(diǎn)擊@SpringBootApplication后可以看到有這么幾個(gè)注解

結(jié)論:springBoot所有的自動(dòng)配置都是在啟動(dòng)的時(shí)候掃描并加載:spring.factories所有的自動(dòng)配置類都在這里面,但是不一定生效,要判斷條件是否成立,只要導(dǎo)入了對(duì)應(yīng)的start,就有了對(duì)應(yīng)的啟動(dòng)器,有了啟動(dòng)器,自動(dòng)裝配就是生效,之后配置成功

@ComponentScan

這個(gè)注解在Spring中非常重要,對(duì)應(yīng)的是XML配置中的元素。

作用:自動(dòng)掃描并加載符合條件的組件或者bean,將這個(gè)bean定義加載到IOC容器中

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

@SpringBootConfiguration

作用:SpringBoot的配置類 ,標(biāo)注在某個(gè)類上 , 表示這是一個(gè)SpringBoot的配置類;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

這里的@Configuration說明這是一個(gè)配置類,這個(gè)配置類就是對(duì)應(yīng)Spring的xml配置文件

@Component說明,啟動(dòng)類本身也是Spring中的一個(gè)組件,負(fù)責(zé)啟動(dòng)應(yīng)用。

@EnableAutoConfiguration

作用:開啟自動(dòng)配置功能

點(diǎn)進(jìn)去后會(huì)看到

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)

然后我們發(fā)現(xiàn)了@AutoConfigurationPackage它的作用是自動(dòng)配置包

@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
}

@import :Spring底層注解@import , 給容器中導(dǎo)入一個(gè)組件

AutoConfigurationPackages.Registrar.class 作用:將主啟動(dòng)類的所在包及包下面所有子包里面的所有組件掃描到Spring容器 ;

我們退回上一步看一下這個(gè)注解

@Import({AutoConfigurationImportSelector.class}) :給容器導(dǎo)入組件 ;

AutoConfigurationImportSelector :自動(dòng)配置導(dǎo)入選擇器,在這個(gè)類中有這么一個(gè)方法

/**
 * Return the auto-configuration class names that should be considered. By default
 * this method will load candidates using {@link SpringFactoriesLoader} with
 * {@link #getSpringFactoriesLoaderFactoryClass()}.
 * @param metadata the source metadata
 * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
 * attributes}
 * @return a list of candidate configurations
 */
//獲得候選的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    //這里的getSpringFactoriesLoaderFactoryClass()
    //返回的是我們最開是看到啟動(dòng)自動(dòng)導(dǎo)入配置文件的注解類;EnableAutoConfiguration
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
         getBeanClassLoader());
   Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
         + "are using a custom packaging, make sure that file is correct.");
   return configurations;
}

在上面這個(gè)方法中調(diào)用了SpringFactoriesLoader這個(gè)類中的靜態(tài)方法,我們查看一下這個(gè)類中的loadFactoryNames這個(gè)方法。

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ClassLoader classLoaderToUse = classLoader;
    if (classLoader == null) {
        classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
    }

    String factoryTypeName = factoryType.getName();
    return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}

發(fā)現(xiàn)他又調(diào)用了loadSpringFactories這個(gè)方法,我們繼續(xù)看

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
    Map<String, List<String>> result = (Map)cache.get(classLoader);
    if (result != null) {
        return result;
    } else {
        HashMap result = new HashMap();
        try {
            Enumeration urls = classLoader.getResources("META-INF/spring.factories");
            while(urls.hasMoreElements()) {
                URL url = (URL)urls.nextElement();
                UrlResource resource = new UrlResource(url);
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                Iterator var6 = properties.entrySet().iterator();
                while(var6.hasNext()) {
                    Entry<?, ?> entry = (Entry)var6.next();
                    String factoryTypeName = ((String)entry.getKey()).trim();
                    String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                    String[] var10 = factoryImplementationNames;
                    int var11 = factoryImplementationNames.length;
                    for(int var12 = 0; var12 < var11; ++var12) {
                        String factoryImplementationName = var10[var12];
                        ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
                            return new ArrayList();
                        })).add(factoryImplementationName.trim());
                    }
                }
            }
            result.replaceAll((factoryType, implementations) -> {
                return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
            });
            cache.put(classLoader, result);
            return result;
        } catch (IOException var14) {
            throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
        }
    }
}

這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories\textcolor{red}{這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories} 這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories

3.2 spring.factories

隨便點(diǎn)一個(gè)看看JerseyAutoConfiguration

會(huì)發(fā)現(xiàn)這都是javaConfig配置類,而且都注入了一些Bean。

所以,自動(dòng)配置真正實(shí)現(xiàn)是從classpath中搜尋所有的META-INF/spring.factories配置文件 ,并將其中對(duì)應(yīng)的 org.springframework.boot.autoconfigure. 包下的配置項(xiàng),通過反射實(shí)例化為對(duì)應(yīng)標(biāo)注了 @Configuration的JavaConfig形式的IOC容器配置類 , 然后將這些都匯總成為一個(gè)實(shí)例并加載到IOC容器中。

4. 結(jié)論

  • springboot在啟動(dòng)的時(shí)候,從類路徑下/META-INF/spring.factories獲取指定的值;
  • 將這些自動(dòng)配置的類導(dǎo)入容器,自動(dòng)配置就會(huì)生效,進(jìn)行自動(dòng)配置;
  • 以前需要自動(dòng)配置的東西,現(xiàn)在springboot幫忙做了;
  • 整合JavaEE,解決方案和自動(dòng)配置的東西都在spring-boot-autoconfigure-2.5.7.jar這個(gè)包下
  • 他會(huì)把所有需要導(dǎo)入的組件,以類名的方式返回,這些組件就會(huì)被添加到容器中
  • 容器中也會(huì)存在非常多的xxxAutoConfiguration的文件(@Bean),就是這些類給容器中的導(dǎo)入這個(gè)場(chǎng)景需要的所有組件,并自動(dòng)配置。@Configuration,javaCOnfig
  • 有了自動(dòng)配置類,免去了我們手動(dòng)編寫配置文件的工作。

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

相關(guān)文章

  • Sharding-jdbc報(bào)錯(cuò):Missing the data source name:‘m0‘解決方案

    Sharding-jdbc報(bào)錯(cuò):Missing the data source 

    在使用MyBatis-plus進(jìn)行數(shù)據(jù)操作時(shí),新增Order實(shí)體屬性后,出現(xiàn)了數(shù)據(jù)源缺失的提示錯(cuò)誤,原因是因?yàn)閡serId屬性值使用了隨機(jī)函數(shù)生成的Long值,這與sharding-jdbc的路由規(guī)則計(jì)算不匹配,導(dǎo)致無法找到正確的數(shù)據(jù)源,通過調(diào)整userId生成邏輯
    2024-11-11
  • 使用spring aop統(tǒng)一處理異常和打印日志方式

    使用spring aop統(tǒng)一處理異常和打印日志方式

    這篇文章主要介紹了使用spring aop統(tǒng)一處理異常和打印日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • IDEA 配置 JRebel 熱部署的方法(推薦)

    IDEA 配置 JRebel 熱部署的方法(推薦)

    這篇文章主要介紹了IDEA 配置 JRebel 熱部署的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot使用Apache Tika實(shí)現(xiàn)多種文檔的內(nèi)容解析

    SpringBoot使用Apache Tika實(shí)現(xiàn)多種文檔的內(nèi)容解析

    在日常開發(fā)中,我們經(jīng)常需要解析不同類型的文檔,如PDF、Word、Excel、HTML、TXT等,Apache Tika是一個(gè)強(qiáng)大的內(nèi)容解析工具,可以輕松地提取文檔中的內(nèi)容和元數(shù)據(jù)信息,本文將通過SpringBoot和Apache Tika的結(jié)合,介紹如何實(shí)現(xiàn)對(duì)多種文檔格式的內(nèi)容解析
    2024-12-12
  • idea運(yùn)行java項(xiàng)目main方法報(bào)build failure錯(cuò)誤的解決方法

    idea運(yùn)行java項(xiàng)目main方法報(bào)build failure錯(cuò)誤的解決方法

    當(dāng)在使用 IntelliJ IDEA 運(yùn)行 Java 項(xiàng)目的 main 方法時(shí)遇到 "Build Failure" 錯(cuò)誤,這通常意味著在項(xiàng)目的構(gòu)建過程中遇到了問題,以下是一些詳細(xì)的解決步驟,以及一個(gè)簡(jiǎn)單的代碼示例,用于展示如何確保 Java 程序可以成功構(gòu)建和運(yùn)行,需要的朋友可以參考下
    2024-09-09
  • 詳解JVM的內(nèi)存對(duì)象介紹[創(chuàng)建和訪問]

    詳解JVM的內(nèi)存對(duì)象介紹[創(chuàng)建和訪問]

    這篇文章主要介紹了JVM的內(nèi)存對(duì)象介紹[創(chuàng)建和訪問],文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java判斷class子類或父類的實(shí)例方法

    java判斷class子類或父類的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于java判斷class子類或父類的實(shí)例方法,需要的朋友們可以參考學(xué)習(xí)下。
    2020-02-02
  • java泛型基本知識(shí)和通用方法

    java泛型基本知識(shí)和通用方法

    這篇文章主要介紹了java泛型基礎(chǔ)知識(shí)及通用方法,從以下幾個(gè)方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類和接口,感興趣的可以了解一下
    2021-06-06
  • Mybatis框架之模板方法模式(Template Method Pattern)的實(shí)現(xiàn)

    Mybatis框架之模板方法模式(Template Method Pattern)的實(shí)現(xiàn)

    MyBatis中使用了模板方法模式來控制SQL語句的執(zhí)行流程,本文主要介紹了Mybatis框架之模板方法模式(Template Method Pattern)的實(shí)現(xiàn),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • SpringBoot淺析緩存機(jī)制之Redis單機(jī)緩存應(yīng)用

    SpringBoot淺析緩存機(jī)制之Redis單機(jī)緩存應(yīng)用

    在上文中我介紹了Spring Boot使用EhCache 2.x來作為緩存的實(shí)現(xiàn),本文接著介紹使用單機(jī)版的Redis作為緩存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08

最新評(píng)論

平罗县| 沧源| 贵德县| 隆安县| 滁州市| 报价| 襄城县| 西畴县| 新河县| 平果县| 天峻县| 改则县| 政和县| 剑川县| 巴林左旗| 凯里市| 文水县| 通榆县| 临汾市| 宁河县| 定日县| 白朗县| 德保县| 永吉县| 馆陶县| 奇台县| 北流市| 高邮市| 砚山县| 浪卡子县| 双峰县| 定襄县| 衡阳县| 司法| 仪陇县| 新昌县| 东光县| 龙里县| 蕉岭县| 杂多县| 张北县|