fix: 新增字典处理

This commit is contained in:
wangyu 2021-03-22 00:29:44 +08:00
parent ee4d57a58f
commit df3748068a
2 changed files with 47 additions and 36 deletions

View File

@ -1,23 +1,43 @@
package com.flyfish.framework.dict.config;
import com.flyfish.framework.dict.service.DictionaryService;
import org.springframework.context.annotation.Bean;
import com.flyfish.framework.dict.annotations.EnableDictProcess;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;
/**
* 字典配置
*
* @author wangyu
*/
public class DictionaryConfig {
public class DictionaryConfig implements ImportBeanDefinitionRegistrar {
public static DictionaryConfig INSTANCE;
private String[] basePackages;
public DictionaryConfig() {
INSTANCE = this;
}
public static String[] basePackages() {
if (null != INSTANCE) {
return INSTANCE.basePackages;
}
return null;
}
/**
* 注册字典处理器开启类扫描注册
* 注册bean定义可以从这里取得元数据自动加载bean
*
* @param dictionaryService 字典服务
* @return 结果
* @param metadata 元数据
* @param registry 注册器
*/
@Bean
public DictionaryProcessor dictionaryProcessor(DictionaryService dictionaryService) {
return new DictionaryProcessor(dictionaryService);
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableDictProcess.class.getCanonicalName(), true);
this.basePackages = (String[]) MapUtils.getObject(attrs, "basePackages");
}
}

View File

@ -1,7 +1,6 @@
package com.flyfish.framework.dict.config;
import com.flyfish.framework.annotations.DictValue;
import com.flyfish.framework.dict.annotations.EnableDictProcess;
import com.flyfish.framework.dict.domain.Dictionary;
import com.flyfish.framework.dict.domain.DictionaryValue;
import com.flyfish.framework.dict.service.DictionaryService;
@ -10,20 +9,21 @@ import com.flyfish.framework.enums.NamedEnum;
import com.flyfish.framework.utils.Assert;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 字典处理器
@ -32,18 +32,20 @@ import java.util.stream.Stream;
* 用于反向注册默认值
*/
@RequiredArgsConstructor
public class DictionaryProcessor implements ImportBeanDefinitionRegistrar {
@Component
public class DictionaryProcessor implements InitializingBean {
private final DictionaryService dictionaryService;
private String[] basePackages;
/**
* spring加载完成后尝试注入值
*/
public void initialize() {
@Override
public void afterPropertiesSet() {
String[] basePackages = DictionaryConfig.basePackages();
Assert.notNull(basePackages, "未指定明确的字典扫描路径!");
Reflections reflections = new Reflections(Stream.of(basePackages).collect(Collectors.toSet()));
Collection<URL> urls = Arrays.stream(basePackages).flatMap(pack -> ClasspathHelper.forPackage(pack).stream())
.collect(Collectors.toList());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(urls)
.setScanners(new FieldAnnotationsScanner()));
Set<Field> fields = reflections.getFieldsAnnotatedWith(DictValue.class);
if (CollectionUtils.isNotEmpty(fields)) {
// 查找是否存在不存在插入存在无视
@ -71,6 +73,7 @@ public class DictionaryProcessor implements ImportBeanDefinitionRegistrar {
}
}
/**
* 映射值转换为字典表的值
*
@ -86,16 +89,4 @@ public class DictionaryProcessor implements ImportBeanDefinitionRegistrar {
}).collect(Collectors.toList());
}
/**
* 注册bean定义可以从这里取得元数据自动加载bean
*
* @param metadata 元数据
* @param registry 注册器
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableDictProcess.class.getCanonicalName(), true);
this.basePackages = (String[]) MapUtils.getObject(attrs, "basePackages");
this.initialize();
}
}