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

SpringBoot詳細(xì)探究講解默認(rèn)組件掃描

 更新時(shí)間:2022年06月09日 09:40:57   作者:望天邊星宿  
在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類(lèi)的同級(jí)目錄下呢

參考視頻:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=6

通過(guò)視頻的學(xué)習(xí)和自身的理解整理出的筆記。

一、前期準(zhǔn)備

1.1 創(chuàng)建工程

創(chuàng)建springboot項(xiàng)目,springboot版本為2.5.0,引入spring-boot-starter-web依賴,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1.2 創(chuàng)建Controller

創(chuàng)建一個(gè)簡(jiǎn)單的Controller用于測(cè)試

@RestController
public class HelloController {
    public void helloController() {
        System.out.println("創(chuàng)建了");
    }
    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }
}

二、探究過(guò)程

2.1 探究目標(biāo)

在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類(lèi)的同級(jí)目錄下呢?

如果我們想要加載不在啟動(dòng)類(lèi)同級(jí)目錄下的bean對(duì)象,需要在啟動(dòng)類(lèi)中使用@ComponentScan注解。

目標(biāo):SpringBoot項(xiàng)目中我們沒(méi)有設(shè)置組件掃描的包,為什么它會(huì)默認(rèn)掃描啟動(dòng)類(lèi)目錄下所有的包。

2.2 探究過(guò)程

2.2.1 回顧容器bean的創(chuàng)建與刷新

在SpringApplication的run()方法中,創(chuàng)建了spring容器context,并通過(guò)refreshContext(context)更新容器加載我們自定義的bean對(duì)象。

我們發(fā)現(xiàn)在執(zhí)行完refreshContext(context)代碼后,自定義的bean對(duì)象(HelloController)就已經(jīng)被創(chuàng)建了,說(shuō)明refreshContext(context)過(guò)程中創(chuàng)建了自定義bean對(duì)象。

下面我們看看究竟是refreshContext(context)中哪些方法創(chuàng)建了自定義bean對(duì)象。

2.2.2 SpringApplication

我接著看refreshContext(context)方法

?? refreshContext()方法

?? refresh()方法

2.2.3 ServletWebServerApplicationContext

再調(diào)用父類(lèi)的refresh()方法

2.2.4 AbstractApplicationContext

?? refresh()方法

在執(zhí)行完這行代碼后創(chuàng)建了自定義bean的beanDefination對(duì)象。下面來(lái)看看這行代碼。

?? invokeBeanFactoryPostProcessors()方法

根據(jù)這個(gè)名字可以看出來(lái)是調(diào)用了bean工廠的后置處理器。

2.2.5 PostProcessorRegistrationDelegate

?? invokeBeanFactoryPostProcessors()方法

調(diào)用bean工廠的后置處理器,這個(gè)方法很長(zhǎng),最終找到了是這行代碼,調(diào)用BeanDefinition注冊(cè)的后置處理。

?? invokeBeanDefinitionRegistryPostProcessors()方法

拿到后置處理器,調(diào)用后置處理器的BeanDefinition注冊(cè)。

2.2.6 ConfigurationClassPostProcessor

?? postProcessBeanDefinitionRegistry()方法

?? processConfigBeanDefinitions()方法

把啟動(dòng)類(lèi)的beanDefinition對(duì)象添加到了configCandidates集合中,后面將要用到。

這行代碼執(zhí)行結(jié)束后就有了helloController。

這個(gè)parser是配置類(lèi)的處理器,通過(guò)傳入很多參數(shù)構(gòu)造了這個(gè)parser處理器。

parser.parse(candidates)中,把啟動(dòng)類(lèi)對(duì)應(yīng)的beanDefinitionHolder對(duì)象傳進(jìn)去了。

下面看看這個(gè)parse方法。

?? parse()方法

2.2.7 ConfigurationClassParser

?? parse()方法

?? processConfigurationClass()方法

?? doProcessConfigurationClass()方法

if (configClass.getMetadata().isAnnotated(Component.class.getName())) { ... }

判斷啟動(dòng)類(lèi)上是否加上了@Component注解,這里的if條件成立。

因?yàn)锧SpringBootApplication包含@SpringBootConfiguration,@SpringBootConfiguration包含@Configuration,@Configuration包含@Component,所以加上了@SpringBootApplication注解就相當(dāng)于加上了@Component注解。

?? processMemberClasses()方法

里面有很多處理各類(lèi)注解的方法

// Process any @PropertySource annotations
// Process any @ComponentScan annotations
// Process any @Import annotations
// Process any @ImportResource annotations
// Process individual @Bean methods

后續(xù)將要對(duì)這個(gè)集合進(jìn)行掃描,那么看看它是如何掃描的。

2.2.8 ComponentScanAnnotationParser

?? parse()方法

ClassUtils.getPackageName(declaringClass):獲取啟動(dòng)類(lèi)所在的包,根據(jù)傳入類(lèi)的全類(lèi)名獲取包名。

scanner.doScan(StringUtils.toStringArray(basePackages)):掃描啟動(dòng)類(lèi)所在的包

2.3 結(jié)論

在容器刷新時(shí)會(huì)調(diào)用BeanFactoryPostProcessor(Bean工廠后置處理器)進(jìn)行處理。其中就有一個(gè)ConfigurationClassPostProcessor(配置類(lèi)處理器)。在這個(gè)處理器中使用ConfigurationClassParser(配置類(lèi)解析器)的parse方法去解析處理我們的配置類(lèi),其中就有對(duì)ComponentScan注解的解析處理。會(huì)去使用ComponentScanAnnotationParser的parse方法去解析。解析時(shí)如果發(fā)現(xiàn)沒(méi)有配置basePackage,它會(huì)去獲取我們加載了注解的這個(gè)類(lèi)所在的包,作為我們的basepackage進(jìn)行組件掃描。

到此這篇關(guān)于SpringBoot詳細(xì)探究講解默認(rèn)組件掃描的文章就介紹到這了,更多相關(guān)SpringBoot組件掃描內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講

    Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講

    這篇文章主要為大家介紹了Spring?Lifecycle和SmartLifecycle的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用

    實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用

    適配器模式的主要作用是在新接口和老接口之間進(jìn)行適配,通過(guò)將一個(gè)類(lèi)的接口轉(zhuǎn)換成客戶期望的另一個(gè)接口,讓原本不兼容的接口可以合作無(wú)間,本文以實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用,需要的朋友可以參考下
    2016-05-05
  • IntelliJ IDEA搜索整個(gè)項(xiàng)目進(jìn)行全局替換(有危險(xiǎn)慎用)

    IntelliJ IDEA搜索整個(gè)項(xiàng)目進(jìn)行全局替換(有危險(xiǎn)慎用)

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA搜索整個(gè)項(xiàng)目進(jìn)行全局替換(有危險(xiǎn)慎用),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • 談?wù)凷pring AOP中@Aspect的高級(jí)用法示例

    談?wù)凷pring AOP中@Aspect的高級(jí)用法示例

    在Spring AOP中目前只有執(zhí)行方法這一個(gè)連接點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Spring AOP中@Aspect的高級(jí)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • SpringBoot返回json和xml的示例代碼

    SpringBoot返回json和xml的示例代碼

    本篇文章主要介紹了SpringBoot返回json和xml的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • springMVC實(shí)現(xiàn)文件上傳和下載

    springMVC實(shí)現(xiàn)文件上傳和下載

    這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)

    SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)

    跨域是指一個(gè)域下的文檔或腳本試圖去請(qǐng)求另一個(gè)域下的資源,或者涉及到兩個(gè)不同域名的資源之間的交互,由于同源策略(Same Origin Policy)的限制,瀏覽器不允許跨域請(qǐng)求,本文小編給大家分享了SpringBoot實(shí)現(xiàn)跨域的幾種常用方式,需要的朋友可以參考下
    2023-09-09
  • 基于Java中進(jìn)制的轉(zhuǎn)換函數(shù)詳解

    基于Java中進(jìn)制的轉(zhuǎn)換函數(shù)詳解

    下面小編就為大家?guī)?lái)一篇基于Java中進(jìn)制的轉(zhuǎn)換函數(shù)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • elasticsearch索引index數(shù)據(jù)功能源碼示例

    elasticsearch索引index數(shù)據(jù)功能源碼示例

    這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • Java轉(zhuǎn)JSON串的幾種方式

    Java轉(zhuǎn)JSON串的幾種方式

    本文給大家總結(jié)一下java轉(zhuǎn)json串的幾種方式,每種方式通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05

最新評(píng)論