定义结构及接口
代码语言:javascript复制public class CodeItem {
private String _itemText;
private Object _itemValue;
public CodeItem(){
}
public CodeItem(String itemText, Object itemValue){
_itemText = itemText;
_itemValue = itemValue;
}
public String get_itemText() {
return _itemText;
}
public void set_itemText(String _itemText) {
this._itemText = _itemText;
}
public Object get_itemValue() {
return _itemValue;
}
public void set_itemValue(Object _itemValue) {
this._itemValue = _itemValue;
}
}
public interface IConvertEnumToCodeItem {
String getCodeName();
}
规范化枚举定义
定义枚举包含name和value字段,实现枚举转换接口,返回代码项名称
代码语言:javascript复制public enum EnableOrDisable implements IConvertEnumToCodeItem {
Enable(100, "启用"), Disabele(300, "禁用");
private int _value;
private String _name;
private EnableOrDisable(int value, String name) {
set_value(value);
set_name((name));
}
public int get_value() {
return _value;
}
public void set_value(int _value) {
this._value = _value;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
@Override
public String toString() {
return _name;
}
@Override
public String getCodeName() {
return "启用禁用";
}
}
扫描转换枚举类
扫描项目包下的所有实现IConvertEnumToCodeItem接口需要转化的枚举类,拿到所有枚举项,调用接口的getCodeName方法获取代码项名称,最后组成代码项
代码语言:javascript复制public class CodeItemUtil {
private static HashMap<String,List<CodeItem>> codeDic = new HashMap<>();
public static void initCodeItem(Class runtimeClass){
List<Class> filterClassList = new ArrayList<>();
filterClassList.add(IConvertEnumToCodeItem.class);
List<Class<?>> enumClassList = ScanUtil.getAllClassByPackageName(runtimeClass.getPackage(),filterClassList);
try{
for (Class clazz:enumClassList) {
if (clazz.isEnum()){
List<CodeItem> codeItemList = new ArrayList<>();
Object[] oo = clazz.getEnumConstants();
for (Object obj : oo) {
String itemText = "";
Object itemValue = null;
List<Field> fs = Arrays.asList(clazz.getDeclaredFields());
for (Field field:fs) {
field.setAccessible(true);
if(field.getName().equals("_name")){
itemText = ConvertOp.convert2String(field.get(obj));
}else if(field.getName().equals("_value")){
itemValue = field.get(obj);
}
}
if(!StringUtil.isEmpty(itemText) && null!=itemValue){
CodeItem codeItem = new CodeItem(itemText,itemValue);
codeItemList.add(codeItem);
}
}
Method getCodeNameMethod = clazz.getDeclaredMethod("getCodeName");
String codeName = ConvertOp.convert2String(getCodeNameMethod.invoke(oo[0]));
if(codeDic.containsKey(codeName)){
throw new Exception("存在同样的枚举代码项,名称:" codeName);
}
codeDic.put(codeName,codeItemList);
}
}
if(codeDic.keySet().size()>0){
System.out.println("扫描并初始化枚举代码项成功,代码项数量:" codeDic.keySet().size() "个");
}
}catch (Exception e){
e.printStackTrace();
}
}
系统启动扫描
新建class实现ApplicationRunner重写run方法,通过Order注解确定执行顺序
代码语言:javascript复制@Component
@Order(value = 99)
public class StartUpContext implements ApplicationRunner {
@Autowired
private ApplicationContext context;
@Override
public void run(ApplicationArguments args) throws Exception {
Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
Class bootClass = annotatedBeans.values().toArray()[0].getClass();
CodeItemUtil.initCodeItem(bootClass);
}
}
实际应用
通用接口
代码语言:javascript复制@RestController
@RequestMapping("/common/enumcode")
public class EnumCodeController extends BaseController {
@ResponseBody
@PostMapping("/getCodeDetail")
public Object getCodeDetail(@RequestBody Map<String, Object> params){
String codeName = ConvertOp.convert2String(params.get("codeName"));
if(StringUtil.isEmpty(codeName)){
return Result.errorResult().add("msg","代码项名称不能为空");
}
List<CodeItem> codeItemList = CodeItemUtil.getCodeList(codeName);
if(null==codeItemList){
return Result.errorResult().add("msg","代码项不存在");
}else{
return Result.okResult().add("obj",codeItemList);
}
}
}
页面调用
代码语言:javascript复制//获取代码项详细
function bindEnumCode(codeName,callback) {
var codeItemUrl = apiFrameUrl "/common/enumcode/getCodeDetail";
var data = {
"codeName": codeName
}
JsonAjax_Sync(JSON.stringify(data), codeItemUrl, function (result, status) {
if (callback) {
callback(result, status);
}
})
}
//获取代码项详细实例
var pageData = {
moduleStatusList: []
}
bindEnumCode("模块状态", function(result, status) {
Vue.set(pageData, "moduleStatusList", result.data.obj);
});
代码语言:javascript复制//列表渲染代码项
Vue.prototype.getCodeText = function (page, codeName, codeValue) {
var text = "";
if (!page[codeName]) {
bindEnumCode(codeName, function (result, status) {
page[codeName] = result.data.obj;
})
}
page[codeName].forEach(element => {
if (element.itemValue == codeValue) {
text = element.itemText
}
});
return text;
}
//列表渲染代码项实例
<el-table-column prop="moduleStatus" align="center" header-align="center" label="模块状态" width="100">
<template slot-scope="scope">
<span v-text="getCodeText(this,'模块状态',scope.row.moduleStatus)"></span>
</template>
</el-table-column>