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

springboot自動裝配之@ComponentScan使用方式

 更新時間:2025年01月23日 15:07:16   作者:qq_39093474  
@componentScan注解用于掃描指定路徑下的組件,并自動將它們注冊為Spring?Bean,該注解支持多種過濾規(guī)則,可以自定義掃描過濾規(guī)則,Spring?Boot通過ConfigurationClassPostProcessor處理@ComponentScan注解,并在啟動時創(chuàng)建和注冊BeanDefinition對象

1.@ComponentScan注解作用

@ComponentScan用于類或接口上主要是指定掃描路徑,spring會把指定路徑下帶有指定注解的類自動裝配到bean容器里。

會被自動裝配的注解包括@Controller、@Service、@Component、@Repository等等。

ComponentScan注解相對應的XML配置就是<context:component-scan/>, 根據(jù)指定的配置自動掃描package,將符合條件的組件加入到IOC容器中;

XML的配置方式如下:

	<context:component-scan
		base-package="com.example.test" use-default-filters="false">
		<context:exclude-filter type="custom"
		expression="com.example.test.filter.MtyTypeFilter" />
	</context:component-scan>

2. @ComponentScan注解屬性

@ComponentScan有如下常用屬性:

  • basePackagesvalue:指定要掃描的路徑(package),如果為空則以@ComponentScan注解的類所在的包為基本的掃描路徑。
  • basePackageClasses:指定具體掃描的類。
  • includeFilters:指定滿足Filter條件的類。
  • excludeFilters:指定排除Filter條件的類。
  • useDefaultFilters=true/false:指定是否需要使用Spring默認的掃描規(guī)則:被@Component, @Repository, @Service, @Controller或者已經(jīng)聲明過@Component自定義注解標記的組件;

在過濾規(guī)則Filter中:

FilterType:指定過濾規(guī)則,支持的過濾規(guī)則有:

  • ANNOTATION:按照注解規(guī)則,過濾被指定注解標記的類(默認);
  • ASSIGNABLE_TYPE:按照給定的類型;
  • ASPECTJ:按照ASPECTJ表達式;
  • REGEX:按照正則表達式;
  • CUSTOM:自定義規(guī)則,自定義的Filter需要實現(xiàn)TypeFilter接口;

value和classes:指定在該規(guī)則下過濾的表達式;

@ComponentScan的常見的配置如下:

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

3. @ComponentScan過濾規(guī)則說明

規(guī)則表達式說明:

  • 1.掃描指定類文件
@ComponentScan(basePackageClasses = Person.class)
  • 2.掃描指定包,使用默認掃描規(guī)則,即被@Component, @Repository, @Service, @Controller或者已經(jīng)聲明過@Component自定義注解標記的組件;
@ComponentScan(value = "com.example")
  • 3.掃描指定包,加載被@Component注解標記的組件和默認規(guī)則的掃描(因為useDefaultFilters默認為true)
@ComponentScan(value = "com.example", includeFilters = { @Filter(type = FilterType.ANNOTATION, value = Component.class) })
  • 4.掃描指定包,只加載Person類型的組件
@ComponentScan(value = "com.example", includeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = Person.class) }, useDefaultFilters = false)
  • 5.掃描指定包,過濾掉被@Component標記的組件
@ComponentScan(value = "com.example", excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Component.class) })
  • 6.掃描指定包,自定義過濾規(guī)則
@ComponentScan(value = "com.example", includeFilters = { @Filter(type = FilterType.CUSTOM, value = MtyTypeFilter.class) }, useDefaultFilters = true)

4. 自定義掃描過濾規(guī)則

用戶自定義掃描過濾規(guī)則,需要實現(xiàn)org.springframework.core.type.filter.TypeFilter接口。

//1.自定義類實現(xiàn)TypeFilter接口并重寫match()方法
public class MtyTypeFilter implements TypeFilter {
    /**
     *
     * @param metadataReader:讀取到當前正在掃描的類的信息
     * @param metadataReaderFactory:可以獲取到其他任何類的信息
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        System.out.println("========MtyTypeFilter===========");
        //獲取當前類的注解的信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        System.out.println("annotationMetadata: "+annotationMetadata);
        //輸出結(jié)果:annotationMetadata: com.example.test.bean.Color

        //獲取當前正在掃描的類的類信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        System.out.println("classMetadata: "+classMetadata);
        //輸出結(jié)果: classMetadata: com.example.test.bean.Color

        //獲取當前類資源(類的路徑)
        Resource resource = metadataReader.getResource();
        System.out.println("resource: "+resource);
        //輸出結(jié)果:resource: file [D:\idea\demo-02\target\classes\com\example\test\bean\Color.class]


        //獲取類名
        String className = classMetadata.getClassName();
        System.out.println("className: "+className);
        //輸出結(jié)果:className: com.example.test.bean.Color
        Class<?> forName = null;
        try {
            forName = Class.forName(className);
            if (Color.class.isAssignableFrom(forName)) {
                // 如果是Color的子類,就加載到IOC容器
                return true;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("========MtyTypeFilter===========");
        return false;
    }
}

5. @ComponentScans

可以一次聲明多個@ComponentScan

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)  //指定ComponentScan可以被ComponentScans作為數(shù)組使用
public @interface ComponentScan {
}
 
 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ComponentScans {
    
	ComponentScan[] value();
 
}
@ComponentScans(value = { @ComponentScan(value = "com.example.test"),
		@ComponentScan(value = "com.example.test", includeFilters = {
				@Filter(type = FilterType.CUSTOM, value = MtyTypeFilter.class) }) })
public class MainConfig {
 
	@Bean(name = "pers", initMethod = "init", destroyMethod = "destory")
	public Person person() {
		return new Person();
	}
 
}

6. spring boot處理@ComponentScan源碼分析

spring創(chuàng)建bean對象的基本流程是先創(chuàng)建對應的BeanDefinition對象,然后在基于BeanDefinition對象來創(chuàng)建Bean對象,SpringBoot也是如此,只不過通過注解創(chuàng)建BeanDefinition對象的時機和解析方式不同而已。

SpringBoot是通過ConfigurationClassPostProcessor這個BeanFactoryPostProcessor類來處理。

本演示的demo涉及到4個演示類,分別是:

  1. 帶有@SpringBootApplication注解的啟動類Demo02Application。
  2. 帶有@RestController注解的類HelloController。
  3. 帶有@Configuration注解且有通過@Bean注解來創(chuàng)建addInterceptors的方法的MyMvcConfig類。
  4. Account實體類無任何注解。

本文的最后會貼出所有代碼。先從啟動類為入口,SpringBoot啟動類如下:

@SpringBootApplication
public class Demo02Application {

    public static void main(String[] args) {
        //1、返回我們IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(Demo02Application.class, args);
	}
}

從:

SpringApplication.run(Demo02Application.class, args);

一路斷點到核心方法

SpringApplication.ConfigurableApplicationContext run(String... args)

run方法干了兩件事:

  • 創(chuàng)建SpringApplication對象
  • 利用創(chuàng)建好的SpringApplication對象調(diào)用run方法
 public ConfigurableApplicationContext run(String... args) {
        long startTime = System.nanoTime();
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        this.configureHeadlessProperty();
        //初始化監(jiān)聽器
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //發(fā)布ApplicationStartingEven
        listeners.starting(bootstrapContext, this.mainApplicationClass);

        try {
        	 //裝配參數(shù)和環(huán)境
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //發(fā)布ApplicationEnvironmentPreparedEvent
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            //創(chuàng)建ApplicationContext,并裝配
            context = this.createApplicationContext();
            context.setApplicationStartup(this.applicationStartup);
            //發(fā)布ApplicationPreparedEvent
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
            }
			//發(fā)布ApplicationStartedEven
            listeners.started(context, timeTakenToStartup);
            //執(zhí)行Spring中@Bean下的一些操作,如靜態(tài)方法
            this.callRunners(context, applicationArguments);
        } catch (Throwable var12) {
            this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }

        try {
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            listeners.ready(context, timeTakenToReady);
            return context;
        } catch (Throwable var11) {
            this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var11);
        }
    }

重點方法一:本方法法實現(xiàn)的重點功能:

  1. demoweb工程,springboot通過反射創(chuàng)建上下文context:AnnotationConfigServletWebServerApplicationContext
  2. 在構(gòu)建context的無參構(gòu)造方法中構(gòu)建成員變量reader=new AnnotatedBeanDefinitionReader(this),在AnnotatedBeanDefinitionReader的無參構(gòu)造方法中會beanFactory對象,并向beanFactory中注冊5個BeanDefinition對象,重點關(guān)注ConfigurationClassPostProcessor。
context = this.createApplicationContext();

重點方法二:本方法實現(xiàn)的重點功能本方法會構(gòu)建啟動類Demo02Application對應的BeanDefinition對象,并注冊到beanFactory中,此時的context對象可見下圖

this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);

重點方法三:本方法實現(xiàn)的重點功能該方法實際調(diào)用applicationContext的refresh方法,本文后面只會分析ConfigurationClassPostProcessor對象的創(chuàng)建和postProcessBeanDefinitionRegistry方法的執(zhí)行

    this.refreshContext(context);
    this.afterRefresh(context, applicationArguments);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);

代碼執(zhí)行后的截圖如下:

ConfigurationClassPostProcessor實現(xiàn)BeanFactoryPostProcessor,關(guān)于BeanFactoryPostProcessor擴展接口的作用在此處不做介紹。

ConfigurationClassPostProcessor對象的創(chuàng)建和方法執(zhí)行的斷點如下:

this.refreshContext(context);–> AbstractApplicationContext.refresh() --> invokeBeanFactoryPostProcessors() -->PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()->invokeBeanDefinitionRegistryPostProcessors()

下面重點看ConfigurationClassPostProcessor類的postProcessBeanDefinitionRegistry()方法如何處理@ComponentScan注解:

同過源代碼發(fā)現(xiàn)最終是由ConfigurationClassParser的解析類來處理,繼續(xù)查看ConfigurationClassParser.doProcessConfigurationClass

原來在這里對@ComponentScan注解做了判斷,上面一段代碼做了核心的幾件事:

  1. 掃描@ComponentScan注解包下面的所有的可自動裝備類,生成BeanDefinition對象,并注冊到beanFactory對象中。
  2. 通過DeferredImportSelectorHandler處理@EnableAutoConfiguration注解,后續(xù)會有專文介紹。
  3. 將帶有@Configuration 注解的類解析成ConfigurationClass對象并緩存,后面創(chuàng)建@Bean注解的Bean對象所對應的BeanDefinition時會用到到此為止MyFilter2對應的BeanDefinition已創(chuàng)建完畢。
  4. 如下圖:

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

富裕县| 醴陵市| 岳阳县| 儋州市| 阿鲁科尔沁旗| 忻城县| 吉隆县| 通山县| 革吉县| 溧水县| 临漳县| 封丘县| 宁德市| 五莲县| 汾西县| 土默特右旗| 东丰县| 报价| 图木舒克市| 英吉沙县| 台北市| 德格县| 宁化县| 高雄县| 韶关市| 惠安县| 昌乐县| 连山| 上饶市| 巧家县| 鹰潭市| 仁寿县| 科技| 肇庆市| 利津县| 铜陵市| 德惠市| 丰城市| 绍兴市| 杭锦后旗| 巴彦淖尔市|