Skip to content

Commit 4efe411

Browse files
committed
优化枚举加载
1 parent c30f878 commit 4efe411

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/configuration/DictionaryProperties.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import lombok.SneakyThrows;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.hswebframework.web.dict.EnumDict;
78
import org.springframework.boot.context.properties.ConfigurationProperties;
89
import org.springframework.core.io.Resource;
@@ -21,25 +22,31 @@
2122
@ConfigurationProperties(prefix = "hsweb.dict")
2223
@Getter
2324
@Setter
25+
@Slf4j
2426
public class DictionaryProperties {
2527

26-
private Set<String> enumPackages=new HashSet<>();
28+
private Set<String> enumPackages = new HashSet<>();
2729

2830
@SneakyThrows
29-
public List<Class> doScanEnum(){
31+
public List<Class> doScanEnum() {
3032
Set<String> packages = new HashSet<>(enumPackages);
3133
packages.add("org.hswebframework.web");
3234
CachingMetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory();
3335
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
3436
List<Class> classes = new ArrayList<>();
3537
for (String enumPackage : packages) {
36-
String path = "classpath*:"+ ClassUtils.convertClassNameToResourcePath(enumPackage)+"/**/*.class";
38+
String path = "classpath*:" + ClassUtils.convertClassNameToResourcePath(enumPackage) + "/**/*.class";
3739
Resource[] resources = resourcePatternResolver.getResources(path);
3840
for (Resource resource : resources) {
39-
MetadataReader reader = metadataReaderFactory.getMetadataReader(resource);
40-
Class clazz=Class.forName(reader.getClassMetadata().getClassName());
41-
if(clazz.isEnum()&& EnumDict.class.isAssignableFrom(clazz)){
42-
classes.add(clazz);
41+
try {
42+
MetadataReader reader = metadataReaderFactory.getMetadataReader(resource);
43+
String name = reader.getClassMetadata().getClassName();
44+
Class clazz = Class.forName(name);
45+
if (clazz.isEnum() && EnumDict.class.isAssignableFrom(clazz)) {
46+
classes.add(clazz);
47+
}
48+
} catch (Throwable e) {
49+
4350
}
4451
}
4552
}

0 commit comments

Comments
 (0)