实现记录业务日志和异常业务日志的操作 springboot( 二 )
5.根据输入方法获取数据字典的数据import java.lang.reflect.Method;public class DictFieldWarpperFactory {public static Object createFieldWarpper(Object parameter, String methodName) {IConstantFactory constantFactory = ConstantFactory.me();try {Method method = IConstantFactory.class.getMethod(methodName, parameter.getClass());return method.invoke(constantFactory, parameter);} catch (Exception e) {try {Method method = IConstantFactory.class.getMethod(methodName, Integer.class);return method.invoke(constantFactory, Integer.parseInt(parameter.toString()));} catch (Exception e1) {throw new RuntimeException("BizExceptionEnum.ERROR_WRAPPER_FIELD");}}} }
6.对应获取数据字典的方法public interface IConstantFactory {/*** 获取状态*/String getWordStatus(Integer DATA_STATUS); }
import com.qihoinfo.dev.log.util.SpringContextHolder;import org.anyline.service.AnylineService;import org.springframework.context.annotation.DependsOn;import org.springframework.stereotype.Component;@Component@DependsOn("springContextHolder")public class ConstantFactory implements IConstantFactory {private AnylineService anylineService = SpringContextHolder.getBean(AnylineService.class);public static IConstantFactory me() {return SpringContextHolder.getBean("constantFactory");}@Overridepublic String getWordStatus(Integer DATA_STATUS) {if ("1".equals(DATA_STATUS.toString())) {return "启用";} else {return "停用";}} }
7.spring根据方法名获取对应容器中的对象import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; /** * Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean */@Componentpublic class SpringContextHolder implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringContextHolder.applicationContext = applicationContext;}public static ApplicationContext getApplicationContext() {assertApplicationContext();return applicationContext;}@SuppressWarnings("unchecked")public static <T> T getBean(String beanName) {assertApplicationContext();return (T) applicationContext.getBean(beanName);}public static <T> T getBean(Class<T> requiredType) {assertApplicationContext();return applicationContext.getBean(requiredType);}private static void assertApplicationContext() {if (SpringContextHolder.applicationContext == null) {throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!");}} }
8.字符串工具类/** * 字符串工具类 */public class StrKit {/*** 首字母变小写*/public static String firstCharToLowerCase(String str) {char firstChar = str.charAt(0);if (firstChar >= 'A' && firstChar <= 'Z') {char[] arr = str.toCharArray();arr[0] += ('a' - 'A');return new String(arr);}return str;}/*** 首字母变大写*/public static String firstCharToUpperCase(String str) {char firstChar = str.charAt(0);if (firstChar >= 'a' && firstChar <= 'z') {char[] arr = str.toCharArray();arr[0] -= ('a' - 'A');return new String(arr);}return str;}/*** 去掉指定后缀*/public static String removeSuffix(String str, String suffix) {if (isEmpty(str) || isEmpty(suffix)) {return str;}if (str.endsWith(suffix)) {return str.substring(0, str.length() - suffix.length());}return str;}/*** 字符串是否为空,空的定义如下 1、为null <br>* 2、为""<br>*/public static boolean isEmpty(String str) {return str == null || str.length() == 0;}}
import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;public class ToolUtil {public static final int SALT_LENGTH = 6;public ToolUtil() {}public static String getExceptionMsg(Throwable e) {StringWriter sw = new StringWriter();try {e.printStackTrace(new PrintWriter(sw));} finally {try {sw.close();} catch (IOException var8) {var8.printStackTrace();}}return sw.getBuffer().toString().replaceAll("\\$", "T");}}
9.获取数据字典的类import java.util.HashMap;public abstract class AbstractDictMap {protected HashMap<String, String> dictory = new HashMap<>();protected HashMap<String, String> fieldWarpperDictory = new HashMap<>();public AbstractDictMap() {put("ID", "主键ID");init();initBeWrapped();}public abstract void init();protected abstract void initBeWrapped();public String get(String key) {return this.dictory.get(key);}public void put(String key, String value) {this.dictory.put(key, value);}public String getFieldWarpperMethodName(String key) {return this.fieldWarpperDictory.get(key);}public void putFieldWrapperMethodName(String key, String methodName) {this.fieldWarpperDictory.put(key, methodName);}}
public class SystemDict extends AbstractDictMap {@Overridepublic void init() {}@Overrideprotected void initBeWrapped() {}}
public class WordMap extends AbstractDictMap {@Overridepublic void init() {put("EN", "英文");put("CN", "中文");put("SHORT", "简称");put("REMARK", "备注");put("DATA_STATUS", "状态");}@Overrideprotected void initBeWrapped() {putFieldWrapperMethodName("DATA_STATUS","getWordStatus");} }
推荐阅读
- 欢乐斗地主怎么看记录 欢乐斗地主如何查看自己的游戏战绩
- 微信搜索记录该咋地才可以查找
- 美团聊天怎样彻底删除记录
- 收款记录删除了还能查到吗
- 收款记录添加备忘对方能看到吗
- 聊天记录删了怎么恢复聊天记录
- 26.30%全球第一 隆基宣布刷新HJT光伏电池记录
- 抖音怎么一键清空聊天记录
- 全民k歌怎么看谁听过的记录
- 退役军人走访记录怎么填写
