java else if or_java-SpringBoot通过Bean代替Ifelse循环

本文介绍如何使用Spring Boot的ClassScanner进行包扫描,并通过自定义注解和过滤器来查找特定类型的类。它展示了如何设置资源加载器,添加过滤规则,并处理异常。

import org.apache.commons.lang3.ArrayUtils;

import org.springframework.beans.factory.BeanDefinitionStoreException;

import org.springframework.context.ResourceLoaderAware;

import org.springframework.core.io.Resource;

import org.springframework.core.io.ResourceLoader;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.core.io.support.ResourcePatternResolver;

import org.springframework.core.io.support.ResourcePatternUtils;

import org.springframework.core.type.classreading.CachingMetadataReaderFactory;

import org.springframework.core.type.classreading.MetadataReader;

import org.springframework.core.type.classreading.MetadataReaderFactory;

import org.springframework.core.type.filter.AnnotationTypeFilter;

import org.springframework.core.type.filter.TypeFilter;

import org.springframework.util.StringUtils;

import org.springframework.util.SystemPropertyUtils;

import java.io.IOException;

import java.lang.annotation.Annotation;

import java.util.HashSet;

import java.util.LinkedList;

import java.util.List;

import java.util.Set;

public class ClassScanner implements ResourceLoaderAware {

private final List includeFilters = new LinkedList();

private final List excludeFilters = new LinkedList();

private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);

@SafeVarargs

public static Set> scan(String[] basePackages, Class extends Annotation>... annotations) {

ClassScanner cs = new ClassScanner();

if (ArrayUtils.isNotEmpty(annotations)) {

for (Class anno : annotations) {

cs.addIncludeFilter(new AnnotationTypeFilter(anno));

}

}

Set> classes = new HashSet<>();

for (String s : basePackages) {

classes.addAll(cs.doScan(s));

}

return classes;

}

@SafeVarargs

public static Set> scan(String basePackages, Class extends Annotation>... annotations) {

return ClassScanner.scan(StringUtils.tokenizeToStringArray(basePackages, ",; \t\n"), annotations);

}

public final ResourceLoader getResourceLoader() {

return this.resourcePatternResolver;

}

@Override

public void setResourceLoader(ResourceLoader resourceLoader) {

this.resourcePatternResolver = ResourcePatternUtils

.getResourcePatternResolver(resourceLoader);

this.metadataReaderFactory = new CachingMetadataReaderFactory(

resourceLoader);

}

public void addIncludeFilter(TypeFilter includeFilter) {

this.includeFilters.add(includeFilter);

}

public void addExcludeFilter(TypeFilter excludeFilter) {

this.excludeFilters.add(0, excludeFilter);

}

public void resetFilters(boolean useDefaultFilters) {

this.includeFilters.clear();

this.excludeFilters.clear();

}

public Set> doScan(String basePackage) {

Set> classes = new HashSet<>();

try {

String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX

+ org.springframework.util.ClassUtils

.convertClassNameToResourcePath(SystemPropertyUtils

.resolvePlaceholders(basePackage))

+ "/**/*.class";

Resource[] resources = this.resourcePatternResolver

.getResources(packageSearchPath);

for (int i = 0; i < resources.length; i++) {

Resource resource = resources[i];

if (resource.isReadable()) {

MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);

if ((includeFilters.size() == 0 && excludeFilters.size() == 0) || matches(metadataReader)) {

try {

classes.add(Class.forName(metadataReader

.getClassMetadata().getClassName()));

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

}

} catch (IOException ex) {

throw new BeanDefinitionStoreException(

"I/O failure during classpath scanning", ex);

}

return classes;

}

protected boolean matches(MetadataReader metadataReader) throws IOException {

for (TypeFilter tf : this.excludeFilters) {

if (tf.match(metadataReader, this.metadataReaderFactory)) {

return false;

}

}

for (TypeFilter tf : this.includeFilters) {

if (tf.match(metadataReader, this.metadataReaderFactory)) {

return true;

}

}

return false;

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值