包路径支持ant风格的模糊匹配类扫描

jfinal中是否有Spring框架中类扫描(PathMatchingResourcePatternResolver)类似的功能?

评论区

JFinal

2024-09-01 19:28

具体啥意思?

无用之人

2024-09-02 09:22

@JFinal spring框架里有PathMatchingResourcePatternResolver可用用来扫描指定包路径下的class,可以根据ant风格模糊匹配某项包下的class文件,现在不想依赖spring框架,jfinal下有类似的类扫描方法吗?代码如下:
public static Class[] scanBasePackage(ScannedClassFilter filter, String... basePackages) {
Set> classSet = new LinkedHashSet<>();
Set resources = getResources(basePackages);
for (Resource resource : resources) {
Class clazz = resolveClassFromResource(resource);
if (filter == null || filter.filter(clazz)) {
classSet.add(clazz);
}
}
return classSet.toArray(new Class[classSet.size()]);
}

private static Set getResources(String[] basePackages) {
Set set = new LinkedHashSet<>();
Resource[] resources;
try {
for (String basePackage : basePackages) {
if (StringUtils.isBlank(basePackage)) {
basePackage = "";
} else {
basePackage = basePackage.replace(".", "/");
if (!basePackage.endsWith("/")) {
basePackage += "/";
}
}
basePackage = "classpath*:" + basePackage + "**/*.class";
resources = resourceLoader.getResources(basePackage);
set.addAll(Arrays.asList(resources));
}
} catch (IOException e) {
throw new RuntimeException("scan basePackage(s) error.", e);
}
return set;
}

JFinal

2024-09-02 12:51

jfinal 有一个 com.jfinal.core.PathScanner,专用于扫描指定包下面的 @Path 注解的,将这部分代码复制出来,稍微改改应该能符合你的需求

https://gitee.com/jfinal/jfinal/blob/master/src/main/java/com/jfinal/core/PathScanner.java

无用之人

2024-09-02 13:51

好的,我下来研究下,感谢。

热门反馈

扫码入社