fix: 新增字典处理
This commit is contained in:
parent
ee4d57a58f
commit
df3748068a
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user