原创

组件注册@ComponentScan的自动扫描和指定扫描规则

扫描组件@ComponentScan可以自动扫描包以内的含有@Service,@Controller,@Repository,@Component的组件到IOC容器里面去

@ComponentScan有几个重要的参数

1.value:填写要扫描的包(会级联扫描内部所有子包)

2.excludeFilters(排除扫描组件的规则)

里面装的是Filter的数组
Filter有2个属性:
①type:type是枚举类,里面有以下几种属性:
ANNOTATION, 注解,
ASSIGNABLE_TYPE, ANNOTATION:指定的类型,按照给定的类型,不管是子类还是实现类
ASPECTJ, 按照Aspectj的表达式,基本上不会用到按照
REGEX, 按照正则表达式
CUSTOM 自定义规则
②value(或者classes): 类类型的数组
按照前面的type定义的规则来找到对应的类进行排除

3.includeFilters,它的属性和excludeFilters是一样的,不同的是,它需要关闭ComponentScan里面的属性(useDefaultFilters = false)

示例代码:

ANNOTATION:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
@ComponentScan(value = "com.debuggg",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
},useDefaultFilters = false)

ASSIGNABLE_TYPE:指定的类及其所有子类,实现类扫描

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
@ComponentScan(value = "com.debuggg",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {UserService.class})
},useDefaultFilters = false)
public class TransferQueueTest {

CUSTOM: 自定义扫描规则

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
@ComponentScan(value = "com.debuggg",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
},useDefaultFilters = false)
public class TransferQueueTest {

MyTypeFilter.java

package com.debuggg.test1.main3;

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 {
    @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();

        System.out.println("----> " + className);
        //如果扫描到的类里面包含er就加入到ioc容器里面
        if(className.contains("er")){
            return true;
        }
        return false;
    }
}
正文到此结束
本文目录