spring 组件 扫描

@ComponentScan(basePackages = {“com”})

组件扫描
@ComponentScan等价于
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com");


模拟包扫描

获取项目真实路径
File file =new File(RealPath)
String[] files= file.list()
clazz = files.for(file.split(.class)[0])
class.forname(clazz)




扫描当前包及其子包

在这里插入图片描述

在这里插入图片描述

根据spring的包表达式进行扫描 classpath*:/com/**/*.class

  • Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);

由源码可见Spring提供了一个资源扫描的类 可以根据spring的包表达式进行扫描


  PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            Resource[] resource = resolver.getResources("classpath*:/com/atsun/**/*.class");
            for (int i = 0; i < resource.length; i++) {
                
                System.out.println(resource[i].getFilename()+"---------------");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



或者

PathMatchingResourcePatternResolver resolver = (PathMatchingResourcePatternResolver)ResourcePatternUtils.getResourcePatternResolver(null);
 try {
     Resource[] resource = resolver.getResources("classpath*:/com/atsun/**/*.class");
     for (int i = 0; i < resource.length; i++) {

         System.out.println(resource[i].getFilename()+"---------------");
     }
 } catch (IOException e) {
     e.printStackTrace();
 }

在这里插入图片描述
TypeFilter

在这里插入图片描述
这里添加的typefilter
org.springframework.context.annotation.ComponentScanAnnotationParser#parse
在这里插入图片描述
确定加了@Compent注解后

  • ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);

在这里插入图片描述
再判断是否有资格成为候选类,由就添加进候选集合
这也就是为什么接口上加了@Component注解还是无法扫描进容器的原因
在这里插入图片描述+ 添加进候选集合
在这里插入图片描述

由源码可以看出,我们也可以借助spring的扫描器来自定义扫描路径,需要将判断条件修改成我们自己的就行了

添加自定义扫描


public class MyScanner extends ClassPathScanningCandidateComponentProvider {


    public MyScanner(){
        this.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });

    }

    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        AnnotationMetadata metadata = beanDefinition.getMetadata();
//	判断类是否有资格作为候选  有可能时内部类 ,接口或者抽象类,有没有加lookup
        return metadata.isInterface();
    }

}


第二种方法添加自定义扫描


@Retention(RetentionPolicy.RUNTIME)
public @interface Sunxxx {
}

public class A extends ClassPathBeanDefinitionScanner {
    public A(BeanDefinitionRegistry registry) {
        super(registry);
    }

    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        AnnotationMetadata metadata = beanDefinition.getMetadata();
		只要是接口就可以
        return metadata.isInterface();

    }

}

A a = new A(context);


//private final List<TypeFilter> includeFilters = new ArrayList<>();


注册	includeFilters 
a.addIncludeFilter(new AnnotationTypeFilter(Sunxxx.class));

int com = a.scan("com");


System.out.println(com);

在这里插入图片描述
mybatis
在这里插入图片描述

  • 自定义就是改变isCandidateComponent方法的判断

再看一下是何时添加的TypeFilter --》 自定义过滤规则 CUSTOM @MapperScanner注解时就有使用
由 new AnnotationConfigApplicationContextnew —> ClassPathBeanDefinitionScanner() 一步步调用过来
在这里插入图片描述

在spring-mybatis中扫描的拓展
在这里插入图片描述
在这里插入图片描述

扫描在哪里执行的?

扫描是在执行BeanDefinitionRegistryPostProcess时执行的,进行扫描的类是他的一个子类ConfigurationClassProcess中

在创建spring应用时,先会调用父类实例化bean工厂

AnnotationConfigApplicationContext extend   GenericApplicationContext
public AnnotationConfigApplicationContext() {
	注册默认bd 
	this.reader = new AnnotatedBeanDefinitionReader(this);
	this.scanner = new ClassPathBeanDefinitionScanner(this);
}

在这里插入图片描述

这里注册默认的5个bd
在这里插入图片描述

在这里插入图片描述

在这个方法中会实例化实现了BeanDefinitionRegistryPostProcessor的类,而ConfigurationClassPostProcess就实现了这个类

org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)

在这里插入图片描述

此时已经有了bd,但是还没在容器中,如果getBean的话就会去创建它
在这里插入图片描述

bean工厂后置处理器


//			ConfigurationClassPostProcessor 是唯一一个实现了BeanDefinitionRegistryPostProcessor的类
//			执行 ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry
//			这个类中完成扫描 解析注解 当解析到import时 mybatis中执行import的类的 一个方法
扫描bd {
	普通注解,
	配置类
}
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);


先获取所有的bd candidateNames  默认5+自己注册的配置类一个


else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
候选类是一个 ImportBeanDefinitionRegistrar -> 委托给它注册额外的 bean 定义
	Class<?> candidateClass = candidate.loadClass();

如果import的类 实现了ImportBeanDefinitionRegistrar接口, 主要是实例化它, 拓展点
	ImportBeanDefinitionRegistrar registrar =
			ParserStrategyUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class,
					this.environment, this.resourceLoader, this.registry);

把实例好的ImportBeanDefinitionRegistrar 放入map中  后面会循环执行
	configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
}

mybatis  就是在执行
ImportBeanDefinitionRegistrar.registerBeanDefinitions方法时,给bd中添加了一个
MapperScannerConfigurer的bd 他实现了 BeanDefinitionRegistryPostProcessor
等spring自己执行到BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry时再来完成扫描






新版中多了一个方式 可以不用@MapperScan注解了
因为MapperScannerConfigurer也实现了 BeanDefinitionRegistryPostProcessor
所以spring就会去执行他的方法
在这里插入图片描述


/**
 * customeimport.properties配置文件中的内容:
 * 		custome.importselector.expression= com.itheima.service.impl.*
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
public class CustomeImportSelector implements ImportSelector {

    private String expression;

    public CustomeImportSelector(){
        try {
            Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("customeimport.properties");
            expression = loadAllProperties.getProperty("custome.importselector.expression");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 生成要导入的bean全限定类名数组
     * @param importingClassMetadata
     * @return
     */
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        //1.定义扫描包的名称
        String[] basePackages = null;
        //2.判断有@Import注解的类上是否有@ComponentScan注解
        if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) {
            //3.取出@ComponentScan注解的属性
            Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
            //4.取出属性名称为basePackages属性的值
            basePackages = (String[]) annotationAttributes.get("basePackages");
        }
        //5.判断是否有此属性(如果没有ComponentScan注解则属性值为null,如果有ComponentScan注解,则basePackages默认为空数组)
        if (basePackages == null || basePackages.length == 0) {
            String basePackage = null;
            try {
                //6.取出包含@Import注解类的包名
                basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //7.存入数组中
            basePackages = new String[] {basePackage};
        }
        //8.创建类路径扫描器
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        //9.创建类型过滤器(此处使用切入点表达式类型过滤器)
        TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader());
        //10.给扫描器加入类型过滤器
        scanner.addIncludeFilter(typeFilter);
        //11.创建存放全限定类名的集合
        Set<String> classes = new HashSet<>();
        //12.填充集合数据
        for (String basePackage : basePackages) {
            scanner.findCandidateComponents(basePackage).forEach(beanDefinition -> classes.add(beanDefinition.getBeanClassName()));
        }
        //13.按照规则返回
        return classes.toArray(new String[classes.size()]);
    }
}



/**
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
@Configuration
@ComponentScan("com.itheima")
@Import(CustomeImportDefinitionRegistrar.class)
public class SpringConfiguration {
}

/**
 * 自定义bean导入注册器
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
public class CustomeImportDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    private String expression;

    public CustomeImportDefinitionRegistrar(){
        try {
            Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("customeimport.properties");
            expression = loadAllProperties.getProperty("custome.importselector.expression");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1.定义扫描包的名称
        String[] basePackages = null;
        //2.判断有@Import注解的类上是否有@ComponentScan注解
        if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) {
            //3.取出@ComponentScan注解的属性
            Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
            //4.取出属性名称为basePackages属性的值
            basePackages = (String[]) annotationAttributes.get("basePackages");
        }
        //5.判断是否有此属性(如果没有ComponentScan注解则属性值为null,如果有ComponentScan注解,则basePackages默认为空数组)
        if (basePackages == null || basePackages.length == 0) {
            String basePackage = null;
            try {
                //6.取出包含@Import注解类的包名
                basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //7.存入数组中
            basePackages = new String[] {basePackage};
        }
        //8.创建类路径扫描器
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
        //9.创建类型过滤器(此处使用切入点表达式类型过滤器)
        TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader());
        //10.给扫描器加入类型过滤器
        scanner.addIncludeFilter(typeFilter);
        //11.扫描指定包
        scanner.scan(basePackages);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值