组件注册——@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

相关文章

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

SpringCloud中的@FeignClient注解使用详解

《SpringCloud中的@FeignClient注解使用详解》在SpringCloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解来标记Feign客户端接口,这篇文章... 在Spring Cloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被