组件注册——@ComponentScan注解

@ComponentScan注解用于自动扫描指定包下的组件。本文详细解析了其使用方式,包括基本的basePackages属性,excludeFilters和includeFilters属性来定制扫描规则,以及如何实现自定义过滤规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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 {
    @Bean
    public 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 {
    @Bean
    public Person person(){
        return new Person("李思",20);
    }
    @Bean
    public 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 {
    @Bean
    public Book book(){
        return new Book("yes",120);
    }
    @Bean
    public Person person(){
        return new Person("张三",12);
    }
}
运行结果:(只会通过类名中含有Book的bean)

 

 

 

 

 

### 关于@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan注解的作用与区别 #### @SpringBootConfiguration @SpringBootConfiguration 是 Spring Boot 中的一个核心注解,主要用于标记一个 Java 类作为配置类。它的主要功能如下: - 它继承自@Configuration注解,因此可以用来定义 Spring 应用程序上下文中使用的 bean。 - 当开发者使用@SpringBootApplication时,默认已经隐含了@SpringBootConfiguration的存在[^2]。 #### @EnableAutoConfiguration @EnableAutoConfiguration 是 Spring Boot 自动化配置的关键所在,其具体作用包括: - 告诉 Spring Boot 根据 classpath 上存在的 jar 文件以及其他条件自动配置应用程序所需的功能。 - 通过 META-INF/spring.factories 文件中的配置,加载对应的自动配置类(由 AutoConfigurationImportSelector 实现)。这些自动配置类能够根据项目中引入的依赖项动态调整配置[^1]。 - 开发者可以通过 `spring.autoconfigure.exclude` 属性来排除某些特定的自动配置类,从而实现更精细的控制[^2]。 #### @ComponentScan @ComponentScan 注解的主要职责在于组件扫描,即自动发现并注册带有特定注解(如@Controller, @Service, @Repository 或 @Component)的类为 Spring Bean。以下是其特点: - 默认情况下,它会扫描标注此注解所在的包及其子包内的所有符合条件的类,并将其纳入 Spring 容器管理之中[^3]。 - 虽然@EnableAutoConfiguration内部也涉及到了一定的组件扫描逻辑(例如通过 AutoConfigurationPackages.Registrar),但它并不完全替代@ComponentScan 的功能[^5]。 尽管@EnableAutoConfiguration 和@ComponentScan 都涉及到某种形式上的“扫描”,但它们的目的不同:前者专注于依据classpath内容决定如何自动化装配各种服务;后者则侧重寻找那些应该被注入容器里的候选对象——也就是所谓的组件实体们。 ```java // 示例代码展示如何联合使用这三个注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 此处包含了@SpringBootConfiguration,@EnableAutoConfiguration以及默认设置下的@ComponentScan public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值