【spring】之基于注解@ComponentScan的一些使用

基于xml形式ComponentScan的使用如下

 

<context:component-scan base-package="com.luna" use-default-filters="false">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

基于注解@ComponentScan的使用

复制代码

@Configuration

/*@ComponentScan(value = "com.luna", includeFilters = {

   // @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
   // @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} )
   @ComponentScan.Filter(type = FilterType.CUSTOM,value = {MyFilter.class})
 },useDefaultFilters = false)*/

@ComponentScan(value = "com.luna", 

    //排除某些类
    excludeFilters = {
       //排除Controller注解标注的类 
    @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
       //排除指定类型的类 
       @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} )
})
public class ScanConfig {
}

复制代码

 

测试

复制代码

@Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = 
                new AnnotationConfigApplicationContext(ScanConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        Arrays.asList(beanDefinitionNames).stream().forEach(x->System.out.println("扫描到的Bean---"+x));
    }

复制代码

 

 

 

@ComponentScan一些常用参数

//基本属性 value/basePackages:指定一组要扫描的包,将扫描这些包及其子包下的文件.(默认基包为配置类所在的包)

classes:直接指定扫描的一些类,这些类所在的包及其子包也会被扫描。

nameGenerator:指定一个默认扫描组件的命名类, 默认命名使用组件类名第一个字小写。

excludeFilters:指定扫描的时候排除某些类,按什么规则,需要一组@ComponentScan.Filter的注解配置,每个@Filter可以配置一组过滤规则,多个@Filter可以基于正则/注解/具体类配置不同的过滤规则。

includeFilters:指定扫描的时候,只包含某些类。

useDefaultFilters 自动扫描Controller Component Service Resposity 这四个注解
所以在不指定明确包的情况下如 com.luna 全包扫描,会扫描到Controller Component Service Resposity 四个注解

 

一、注解说明

@ComponentScan:会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类

它里面的属性: value指定扫描的包,includeFilters包含那些过滤,excludeFilters不包含那些过滤,useDefaultFilters默认的过滤规则是开启的,如果我们要自定义的话是要关闭的。其中@Filters是一个过滤器的接口。

@Filters 指过滤规则,FilterType指定过滤的规则(

            FilterType.ANNOTATION:按照注解

            FilterType.ASSIGNABLE_TYPE:按照给定的类型;

            FilterType.ASPECTJ:使用ASPECTJ表达式

            FilterType.REGEX:使用正则指定

            FilterType.CUSTOM:使用自定义规则)

            classes指定过滤的类

示列如下:

package com.guang.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import com.guang.entity.Person;
 
@Configuration
// @ComponentScan("包路径") 会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类
// includeFilters 指定包含扫描的内容
// excludeFilters 指定不包含的内容
// @Filter 指定过滤规则,type指定扫描的规则(注解,正则,自定义,ASPECTJ表达式),classes指定的扫描的规则类
@ComponentScan(basePackages = {"com.guang"},
        includeFilters = @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
        excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = {Repository.class}),
        includeFilters = @Filter(type = FilterType.CUSTOM, classes = {FilterCustom.class}),
        useDefaultFilters = false)
public class Myconfig {
 
    @Bean("person")
    public Person person01() {
        return new Person("aiha", 25);
    }
 
}
二、如果我们在使用自定义(includeFilters = @Filter(type = FilterType.CUSTOM, classes = {自己定义的类}))过滤规则的时候,我们自己定义的类要实现TypeFilter接口,例如:

package com.guang.config;
 
import java.io.IOException;
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;
 
/**
 * 自定义的过滤规则,在自动扫描的时候用,自定义过滤的时候不在使用默认的规则了
 *
 * @ClassName FilterCustom
 * @author xxx
 * @date xxx
 * @version xxx
 */
public class FilterCustom implements TypeFilter {
 
    /**
     * 
     * @Title: match
     * @Description: 覆盖方法注释标签说明
     * @param metadataReader 读取的当前正在扫描类的信息
     * @param metadataReaderFactory 类工厂中其它类的信息
     * @return
     * @throws IOException
     * @see org.springframework.core.type.filter.TypeFilter#match(org.springframework.core.type.classreading.MetadataReader,
     *      org.springframework.core.type.classreading.MetadataReaderFactory)
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
            throws IOException {
        // 获取当前类注解的信息
        // AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
        // 获取当前正在扫描类的信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 获取当前类路径的信息
        // Resource resource = metadataReader.getResource();
        if (classMetadata.getClassName().startsWith("person")) {
            return true;
        }
        return false;
    }
 
}

-

FilterType.ANNOTATION(默认)一组注解,命中所有使用该注解的类,{MyAnno.class}-
FilterType.ASSIGNABLE_TYPE一组具体类-
FilterType.ASPECTJ-一组表达式,使用Aspectj表达式命中类
FilterType.REGEX-一组表达式,使用正则命中类
FilterType.CUSTOM自定义的TypeFilter.-
@ComponentScanSpring框架提供的一个注解,用于告诉Spring容器应该扫描哪些包及其下的组件(如@Controller、@Service、@Repository等)。通过这种方式,你可以简化Spring配置,让Spring自动发现并管理那些应用组件。 使用@ComponentScan的基本语法如下: ```java @Configuration @ComponentScan(basePackages = "com.example.package1, com.example.package2") public class AppConfig { // 其他配置 } ``` - `@Configuration`: 这个注解表明AppConfig是一个配置类,Spring会查找其上的@Bean注解来实例化。 - `basePackages`: 设置要扫描的包名列表。这里是`com.example.package1` 和 `com.example.package2`,你可以添加多个逗号分隔的包名,表示多个路径。 在这个例子中,Spring将搜索这两个包及其所有子包下的@Component、@Service、@Repository、@Controller等注解的类,并将其作为Spring管理的对象自动注册。 注意: - 避免扫描太多层级,因为过多的扫描可能会导致性能下降。 - 可以配合@EnableAutoConfiguration使用,当启用了自动配置,Spring Boot会自动寻找@ComponentScan,并基于你的环境选择合适的配置。 使用@ComponentScan时,如果希望只扫描特定类型的组件,可以添加`includeFilters`属性,比如: ```java @ComponentScan( basePackages = "com.example", includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {YourCustomType.class}) ) ``` 这将会扫描`com.example`包下所有能赋值给`YourCustomType`的类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值