ClassPathScanningCandidateComponentProvider

先看一个样例:

ScanningService1.java
package com.dzm.spring;


public class ScanningService1 {
}


ScanningService2.java


package com.dzm.spring;

import org.springframework.stereotype.Service;


@Service
public class ScanningService2 {
}


Test.java

package com.dzm;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;

import java.util.Set;


public class Test {
public static void main(String[] args) throws Exception {

ClassPathScanningCandidateComponentProvider p = new ClassPathScanningCandidateComponentProvider(true);
Set<BeanDefinition> set = p.findCandidateComponents("com.dzm.spring");
for (BeanDefinition beanDefinition : set) {
System.out.println(beanDefinition.getBeanClassName());
}
}
}

输出:
com.dzm.spring.ScanningService2

从输出的结果看,在包com.dzm.spring里面有两个类,使用@Service标注的类被解析了出来。

通过ClassPathScanningCandidateComponentProvider(true)的构造器,就可以扫描出任何出任何我们添加了@Component annotation的类,它是怎么做到的呢? 我们来看一下这个构造方法都做了什么:
this.includeFilters.add(newAnnotationTypeFilter(Component.class));
当我们在构造方法里面传入了true之后,对象在它自己的includeFilters属性里面添加了一个AnnotationTypeFilter对象,并且此对象的参数是Component annotation,这就是为什么我们能扫描到@Service的类了。

ClassPathScanningCandidateComponentProvider有两个属性:
private finalList<TypeFilter>includeFilters=newLinkedList<TypeFilter>();


private finalList<TypeFilter>excludeFilters=newLinkedList<TypeFilter>();

一个是包含的过滤器,一个是排除的过滤器,在扫描的时候,首先进行排除过滤器的匹配,如果匹配了结果集中就不包含此类,否则继续包含过滤器的匹配,如果匹配了就包含此类。

既然是这样,那么我们自己写一个继承ClassPathScanningCandidateComponentProvider的类,然后声明一个自己的annotation,然后扫描特定的包,我们也能扫描出相应的类名,然后使用Java反射机制,就可以构造出自己想要的对象。

同时这里的TypeFilter我们也可以进行相应的定制,不仅仅局限于annotation

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\BJJGSWGLJ\hd\wisdom-city-common\target\classes\com\louddt\asset\wisdom\entity\common\dict\Dict.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [E:\BJJGSWGLJ\hd\wisdom-city-common\target\classes\com\louddt\asset\wisdom\entity\common\dict\Dict.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61 at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:452) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:315) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationCl
最新发布
08-01
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/19242/.m2/itsp-repository/org/springframework/boot/spring-boot-autoconfigure/2.7.12/spring-boot-autoconfigure-2.7.12.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake) at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:457) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:316) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249) ~[spring-context-5.3.27.jar:5.3.27] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:206) ~[spring-context-5.3.27.jar:5.3.什么问题
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值