Spring注解@ComponentScan

Spring注解@ComponentScan

一、@ComponentScan注解
1、@ComponentScan注解作用

@ComponentScan用于扫描指定包下的类,将标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类扫描到Spring容器,作为Spring
Bean

2、@ComponentScan使用实例
(1)项目结构

在这里插入图片描述

(2)标注有标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类
@Controller
public class Book {
}

@Service
public class Persion {
}

@Repository
public class Student {
}

@Component
public class User {
}
(3)标注有@ComponentScan注解的配置类
@Configuration
/**
 * @ComponentScan不指定value的话,默认扫描当前配置类所在的包
 */
@ComponentScan(value = "com.dashu")
public class BeanConfig {

}

(4)测试类
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class Main {

    public static void main(String[] args) {
        /**
         * java配置类包扫描
         */
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        String[] beanDefinitionNames2 = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames2) {
            System.out.println(name);
        }
    }

}
(5)测试结果

在这里插入图片描述

二、@ComponentScan注解扫描规则

根据参数includeFilters指定包含扫描哪些组件

根据参数excludeFilters指定排除扫描哪些组件

在这里插入图片描述

三、排除excludeFilters

1、常用排除规则
//按照注解来指定规则
FilterType.ANNOTATION
//按照给定的类型来指定规则
FilterType.ASSIGNABLE_TYPE
2、排除案例
(1)标注有@ComponentScan注解的配置类,并指定两个常用的排除规则
@Configuration
/**
 * @ComponentScan不指定value的话,默认扫描当前配置类所在的包
 */
@ComponentScan(value = "com.dashu",excludeFilters = {
        //按照注解的方式,排除标有注解@Controller的类
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
        //按照指定类型,排除Persion类
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Persion.class})
})
public class BeanConfig {

}
(2) 测试结果

在这里插入图片描述

四、指定包含includeFilters

1、常用包含规则
//按照注解来指定规则
FilterType.ANNOTATION
//按照给定的类型来指定规则
FilterType.ASSIGNABLE_TYPE
    
//使用指定包含规则,需要禁用掉默认的扫描规则
useDefaultFilters = false
2、指定包含案例
(1)标注有@ComponentScan注解的配置类,并指定两个常用的指定包含规则
@Configuration
/**
 * @ComponentScan不指定value的话,默认扫描当前配置类所在的包
 */
@ComponentScan(value = "com.dashu",

        includeFilters = {
        //按照注解的方式,排除标有注解@Controller的类
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
        //按照指定类型,排除Persion类
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Persion.class})},

        //禁用掉默认的扫描规则
        useDefaultFilters = false)
public class BeanConfig {

}

(2) 测试结果

在这里插入图片描述

五、FilterType规则
public enum FilterType {

	/**
		注解
	 */
	ANNOTATION,

	/**
	给定类型
	 */
	ASSIGNABLE_TYPE,

	/**
	ASPECTJ表达式
	 */
	ASPECTJ,

	/**
	正则表达式
	 */
	REGEX,

	/** 
	自定义规则
	 */
	CUSTOM

}

六、自定义规则

/** Filter candidates using a given custom
 * {@link org.springframework.core.type.filter.TypeFilter} implementation.
 */
CUSTOM

自定义规则必须是TypeFilter的实现类

1、项目结构

在这里插入图片描述

2、创建自定义规则类 MyTypeFilter
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 {

    /**
     *
     * @param metadataReader    读取到当前正在扫描类的信息
     * @param metadataReaderFactory 可以获取到其他任何类的信息
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {

        //获取当前正在扫描的类的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

        //获取当前正在扫描的类的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();

        //获取当前正在扫描的类的资源信息
        Resource resource = metadataReader.getResource();

        //获取当前类名
        String className = classMetadata.getClassName();

        //判断当前类名是否包含Persion,包含Persion则返回true,将该类扫描到容器,否则返回false过滤
        if (className.indexOf("Persion") != -1){

            return true;
        }

        return false;
    }
}
3、标注有@ComponentScan注解的配置类,并指定自定义规则
org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;


@Configuration
/**
 * @ComponentScan不指定value的话,默认扫描当前配置类所在的包
 */
@ComponentScan(value = "com.dashu",

        includeFilters = {
        //自定义规则
        @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})},

        //禁用掉默认的扫描规则
        useDefaultFilters = false)
public class BeanConfig {

}
4、测试结果

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值