|
@@ -9,9 +9,16 @@ import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.core.type.classreading.MetadataReaderFactory;
|
|
|
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* todo 考虑编程界面
|
|
@@ -23,33 +30,53 @@ import java.io.IOException;
|
|
|
public class EnumDictionaryRegisterConfig {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
@Bean
|
|
|
public DictionaryEnumRepositoryCustomizer dictionaryEnumRepositoryCustomizer() {
|
|
|
return dictionaryEnumStore -> {
|
|
|
- try {
|
|
|
- // 加載資源 org.lym.pom.repository.mongo.enums 下的 所有.class文件
|
|
|
- String packageName = "org.lym.pom.repository.mongo.enums";
|
|
|
-
|
|
|
- PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
- packageName = packageName.replace('.', '/');
|
|
|
- String classPath = "classpath*:" + packageName + (packageName.endsWith("/") ? "" : "/") + "**/*.class";
|
|
|
- Resource[] resources = resolver.getResources(classPath);
|
|
|
- int count = 0;
|
|
|
- for(Resource res :resources) {
|
|
|
- // 先获取resource的元信息,然后获取class元信息,最后得到 class 全路径
|
|
|
- String clsName = new SimpleMetadataReaderFactory().getMetadataReader(res).getClassMetadata().getClassName();
|
|
|
- // 通过名称加载
|
|
|
- Class<?> clazz = Class.forName(clsName);
|
|
|
- if(clazz.isEnum() && DictionaryEnum.class.isAssignableFrom(clazz)) {
|
|
|
+ // 加載資源 org.lym.pom.repository.mongo.enums 下的 所有.class文件
|
|
|
+ AtomicInteger count = new AtomicInteger();
|
|
|
+ loadClassInPackage("org.lym.pom.repository.mongo.enums",
|
|
|
+ clazz -> clazz.isEnum() && DictionaryEnum.class.isAssignableFrom(clazz),
|
|
|
+ clazz -> {
|
|
|
dictionaryEnumStore.register((Class<? extends Enum<? extends DictionaryEnum<?, ?>>>) clazz);
|
|
|
- logger.info("register " + clsName + " to DefaultDictionaryEnumStore.");
|
|
|
- count++;
|
|
|
- }
|
|
|
- }
|
|
|
- logger.info("register " + count + " classes DefaultDictionaryEnumStore.");
|
|
|
- } catch (IOException|ClassNotFoundException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
+ logger.info("register " + clazz.getName() + " to DefaultDictionaryEnumStore.");
|
|
|
+ count.incrementAndGet();
|
|
|
+ });
|
|
|
+ logger.info("register " + count.get() + " classes DefaultDictionaryEnumStore.");
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+ public static List<Class<?>> loadClassInPackage(String packageName, Function<Class<?>, Boolean> classFilter,
|
|
|
+ Consumer<Class<?>> consumer) {
|
|
|
+
|
|
|
+ return Arrays.stream(EnumDictionaryRegisterConfig.readPackageAllClassResource(packageName))
|
|
|
+ .map(EnumDictionaryRegisterConfig::readResourceToClass)
|
|
|
+ .filter(classFilter::apply)
|
|
|
+ .peek(consumer)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
|
|
|
+
|
|
|
+ public static Resource[] readPackageAllClassResource(String packageName) {
|
|
|
+ try {
|
|
|
+ PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
+ packageName = packageName.replace('.', '/');
|
|
|
+ String classPath = "classpath*:" + packageName + (packageName.endsWith("/") ? "" : "/") + "**/*.class";
|
|
|
+ return resolver.getResources(classPath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Class<?> readResourceToClass(Resource res) {
|
|
|
+ try {
|
|
|
+ // 先获取resource的元信息,然后获取class元信息,最后得到 class 全路径,通过名称加载
|
|
|
+ String clsName = metadataReaderFactory.getMetadataReader(res).getClassMetadata().getClassName();
|
|
|
+ return Class.forName(clsName);
|
|
|
+ } catch (IOException | ClassNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|