Spring boot 打jar包启动后无法正常扫描class的问题(Idea直接运行正常)
2021-05-28 08:01
阅读:641
标签:lists etc att cat 无法 res return rgba one
import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.util.ClassUtils; import java.io.IOException; import java.util.Collections; import java.util.List; /** * 注意,如果是jar包启动的,需要打包的时候加上true */ @Slf4j public class ClassUtil { public static List> getAllClasses(Class> c) { String packageName = c.getPackage().getName(); return getClasses(packageName); } /** * 根据包名查询出所有子类信息 * */ private static List > getClasses(String packageName) { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); final String RESOURCE_PATTERN = "/**/*.class"; // 扫描的包名 List > classCache = Lists.newArrayList(); try { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(packageName) + RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); //扫描到的class String className = reader.getClassMetadata().getClassName(); Class> clazz = Class.forName(className); classCache.add(clazz); } } return classCache; } catch (ClassNotFoundException | IOException e) { log.error("读取class失败", e); } return Collections.emptyList(); } }
Spring boot 打jar包启动后无法正常扫描class的问题(Idea直接运行正常)
标签:lists etc att cat 无法 res return rgba one
原文地址:https://www.cnblogs.com/gabin/p/14788433.html
上一篇:Java_String类
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:Spring boot 打jar包启动后无法正常扫描class的问题(Idea直接运行正常)
文章链接:http://soscw.com/index.php/essay/88581.html
文章标题:Spring boot 打jar包启动后无法正常扫描class的问题(Idea直接运行正常)
文章链接:http://soscw.com/index.php/essay/88581.html
评论
亲,登录后才可以留言!