进入正题之前先说说自己为什么要弄这么一个功能,怎么就“贱到”非要用枚举类去注入Service呢?
业务需求:做的交通统计图表的功能,要求不同的业务会有不同的指标,每个指标都是一个监测的图表。每个指标又是可以在不同的业务内
指标数量是固定,业务数量也是相对固定了。而我就想当然就枚举来控制筛选了,不曾想“灾难”来了。每个业务+指标就是一个库表服务啊。但是既然这个X已经装出来了,断然没有不实践就放弃的(果然实践后发现装的有点大,第一次填坑失败!)
最直观的想法是没问题的,那就是每个枚举都给来个Service实例:
public enum BusinessIndexEnum2 {
/**
* 两客一危-微观-实时客运量
*/
PASSENGER_MICR_TRAFFIC_NUM("两客一危", "201", "实时客运量",new PassengerHourTrafficServiceImpl());
private String name;
private String indexCode;
private String tableName;
private StatsService service;
BusinessIndexEnum2(String name, String indexCode, String tableName, StatsService service) {
this.name = name;
this.indexCode = indexCode;
this.tableName = tableName;
this.service = service;
}
public static StatsService getServiceImpl(String name, String indexCode) {
if (StringUtils.isEmpty(name) || StringUtils.isEmpty(indexCode)) {
throw new CommonException("参数不能为空");
}
BusinessIndexEnum2[] values = BusinessIndexEnum2.values();
StatsService baseService = null;
for (BusinessIndexEnum2 businessIndexEnum : values) {
String code = businessIndexEnum.getIndexCode();
String enumName = businessIndexEnum.getName();
if (code.equals(indexCode) && name.contains(enumName)) {
baseService = businessIndexEnum.getService();
break;
}
}
return baseService;
}
/**
* 省略get/set方法
*/
}
最后打脸了,获取Dao那是null了,失败了!强调一下,这个思路是没错的,只是本人技能发挥是失误(就是能力不行)。
最终,经过本人各种烧脑回忆各种知识(其实就是百度一下),终于是把Service给成功注入进去了,其实是和工厂模式差不多了。
package com.techcomer.monitor.subjectstatistics.enums;
import com.techcomer.common.mybatis.exception.CommonException;
import com.techcomer.common.tools.utils.StringUtils;
import com.techcomer.monitor.subjectstatistics.service.PassengerHourTrafficService;
import com.techcomer.monitor.subjectstatistics.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.EnumSet;
/**
* @author huald
* @date 2020/9/14
*/
public enum BusinessIndexEnum {
/**
* 两客一危-微观-实时客运量
*/
PASSENGER_MICR_TRAFFIC_NUM("两客一危", "201", "实时客运量"){
private ApplicationContext bean;
@Override
public void setBean(ApplicationContext bean) {
this.bean = bean;
this.setService(bean.getBean(PassengerHourTrafficService.class));
}
};
private String name;
private String indexCode;
private String tableName;
private StatsService service;
BusinessIndexEnum(String name, String indexCode, String tableName, StatsService service) {
this.name = name;
this.indexCode = indexCode;
this.tableName = tableName;
this.service = service;
}
BusinessIndexEnum2(String name, String indexCode, String tableName) {
this.name = name;
this.indexCode = indexCode;
this.tableName = tableName;
}
public static StatsService getServiceImpl(String name, String indexCode) {
if (StringUtils.isEmpty(name) || StringUtils.isEmpty(indexCode)) {
throw new CommonException("参数不能为空");
}
BusinessIndexEnum2[] values = BusinessIndexEnum2.values();
StatsService baseService = null;
for (BusinessIndexEnum2 businessIndexEnum : values) {
String code = businessIndexEnum.getIndexCode();
String enumName = businessIndexEnum.getName();
if (code.equals(indexCode) && name.contains(enumName)) {
baseService = businessIndexEnum.getService();
break;
}
}
return baseService;
}
public abstract void setBean(ApplicationContext bean);
@Component
public static class EnumTypeServiceInjector {
@Autowired
private ApplicationContext bean;
@PostConstruct
public void postConstruct() {
for (BusinessIndexEnum2 type : EnumSet.allOf(BusinessIndexEnum2.class)) {
type.setBean(bean);
}
}
}
/**
*省略get/set方法
*/
}
最终这个X还是装成了,坚持不懈的回报!
此功能可以结合 前后端分离之接口参数自定义通用查询 使用,这是一款基于Mybatis-plus插件查询功能的二次开发,让你的查询变得更加简单,简洁,方便和灵活。从此告别和前端人员的频繁沟通!!效果更佳哦!