组件注册——@ComponentScan注解

2023-12-24 23:50

本文主要是介绍组件注册——@ComponentScan注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@ComponentScan注解用于自动扫描指定包下的所有组件,也可以通过添加属性值来指定扫描规则。

1、@ComponentScan(basePackages="包名"),最简单的使用方法,扫描包名下的所有组件。

项目结构

 

MainConfig类内容,该类是一个配置类,相当于一个xml配置文件。@ComponentScan(basePackages = "com.wyx.controller")表示会扫描com.wyx.controller包下的所有组件,并将这些组件添加到IOC容器中。

package com.wyx.config;import com.wyx.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages = "com.wyx.controller" )
public class MainConfig {@Beanpublic Person person(){return new Person("李思",20);}
}

MainTest类的内容,这是一个测试类,输出容器中所有的bean,运行程序会发现,容器中有controller包下的bean。
package com.wyx;import com.wyx.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {public static void main(String[] args){ApplicationContext atcApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);String[] beanDefinitionNames = atcApplicationContext.getBeanDefinitionNames();for(String name:beanDefinitionNames){System.out.println(name);}}
}

运行结果

2、excludeFilters = {@ComponentScan.Filter(type= FilterType.***,classes = ***.class),...},指定扫描规则,去除指定注解的组件。

如源码所示,excludeFilters属性是一个@ComponentScan.Filter注解数组,每个@ComponentScan.Filter之间用逗号分隔。

@ComponentScan.Filter的源码如下图所示,有type、classes、value三个属性

如下图所示,先来看type属性,它是FilterType类型的,而FilterType是一个枚举类型,一种有五个值。分别是FitlerType.ANNOTATION:指定注解

FilterType.ASSIGNABLE_TYPE:指定类型

FilterType.ASPECTJ:Aspectj语法指定,很少使用

FilterType.REGEX:使用正则表达式指定

FilterType.CUSTOM:自定义规则

classes属性则是一个运行时类型Class对象。

实例:项目结构同上
MainConfig类代码如下:@ComponentScan.Filter(type= FilterType.ANNOTATION,value = Repository.class) 去除@Repository注解组件@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,value=Person.class) }) 去除Person类Bean

package com.wyx.config;import com.wyx.domain.Book;
import com.wyx.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Repository;@Configuration
@ComponentScan(basePackages = "com.wyx" ,excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = Repository.class),@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,value=Person.class)
})
public class MainConfig {@Beanpublic Person person(){return new Person("李思",20);}@Beanpublic Book book(){return new Book("十万个为什么",160);}
}测试类同上面MainTest类

程序运行结果:

这里有一个注意点,FilterType.ASSIGNABLE_TYPE只能识别到由Component注解的普通bean,无法去除通过java配置类注入的bean。

3、includeFilters = {@ComponentScan.Filter(type= FilterType.***,classes = ***.class),...},指定扫描规则,只注入符合注解条件的组件。

这个与excludeFilters用法相似,但是有一个注意点,用这个属性时,需要添加一个属性。因为spring默认是注入所有扫描到的组件,所以要将useDefaultFilters 的属性值设置为false,includeFilters才能生效。

MainConfig类做如下修改

@Configuration
@ComponentScan(basePackages = "com.wyx" ,includeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = Repository.class),@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,value=Person.class)
},useDefaultFilters = false)

只扫描Reposity注解和Person类

运行结果:

4、自定义规则,需要编写一个类,该类实现了TypeFilter接口的类,下面是我自己定义的MyTypeFilter类,当该类返回true时,会选中该组件。TypeFilter接口中有一个抽象方法:match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory),当中有两个形参:

MetadataReader metadataReader 读取到当前正在扫描的类信息
MetadataReaderFactory metadataReaderFactory 可以获取到其他任何类的信息MyTypeFilter类代码如下:package com.wyx.config;import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;import java.io.IOException;public class MyTypeFilter implements TypeFilter {public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {//当前类的注解信息AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();//当前正在扫描的类的信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//当前类的资源,比如类的路径Resource resource = metadataReader.getResource();if(classMetadata.getClassName().contains("Book")) {return true;}return false;}

配置类如下所示

package com.wyx.config;import com.wyx.domain.Book;
import com.wyx.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;@Configuration
@ComponentScan(basePackages = "com.wyx" ,includeFilters = {@ComponentScan.Filter(type=FilterType.CUSTOM,classes = MyTypeFilter.class)},useDefaultFilters = false
)
public class MainConfig {@Beanpublic Book book(){return new Book("yes",120);}@Beanpublic Person person(){return new Person("张三",12);}
}
运行结果:(只会通过类名中含有Book的bean)

 

 

 

 

 

这篇关于组件注册——@ComponentScan注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/533567

相关文章

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

Java Jackson核心注解使用详解

《JavaJackson核心注解使用详解》:本文主要介绍JavaJackson核心注解的使用,​​Jackson核心注解​​用于控制Java对象与JSON之间的序列化、反序列化行为,简化字段映射... 目录前言一、@jsonProperty-指定JSON字段名二、@JsonIgnore-忽略字段三、@Jso

Spring Boot 常用注解详解与使用最佳实践建议

《SpringBoot常用注解详解与使用最佳实践建议》:本文主要介绍SpringBoot常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、核心启动注解1. @SpringBootApplication2. @EnableAutoConfi

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringRetry重试机制之@Retryable注解与重试策略详解

《SpringRetry重试机制之@Retryable注解与重试策略详解》本文将详细介绍SpringRetry的重试机制,特别是@Retryable注解的使用及各种重试策略的配置,帮助开发者构建更加健... 目录引言一、SpringRetry基础知识二、启用SpringRetry三、@Retryable注解