微服务(一)--Common
来源:互联网 发布:淘宝美工负责什么工作 编辑:程序博客网 时间:2024/06/10 21:31
目录
一个完整的springCloud项目怎么去搭建
首先我们看看 Common包
枚举类
通用
public interface IUnexEnum<E extends Enum<?>> { * 获取code public String code(); * 获取name public String desc(); * 判断code是否相等 public boolean equals(String code);}
怎么使用 他呢
public poEnum implents IUnexEnum<poEnum>{ we("我们","我们"), our("我们的","我们的"); private final String code; private final String desc; @Override public boolean equals(String code) { return this.code.equals(code); } private poEnum(String code, String desc) { this.code = code; this.desc = desc; }}
异常
Throwable 是 Error 和Exception的父类:
我们真的有需求要在序列化后添加一个字段或者方法呢?应该怎么办?那就是自己去指定serialVersionUID,防止版本升级时反序列化出错,
public class UnexBaseException extends RuntimeException{ // serialVersionUID private static final long serialVersionUID = 8458544317507845657L; // 异常编码 private String errorCode; // 异常信息资源ID private String messageId; // 异常信息参数 private Object[] messageArgs; // 构造方法 public UnexBaseException(){ } /** * 用指定的cause异常构造一个新的BaseException * * @param cause the exception cause */ public UnexBaseException(Throwable cause){ super(cause); } /** * 用指定的messageText异常构造一个新的BaseException * * @param messageText 异常信息文本 */ public UnexBaseException(String messageText){ super(messageText); } /** * 用指定的messageText异常构造一个新的BaseException * * @param messageText 异常信息文本 * @param cause 异常对象 */ public UnexBaseException(String messageText, Throwable cause){ super(messageText, cause); } /** * 用指定code和异常日志 */ public UnexBaseException(String errorCode, Throwable cause, String messageId, Object[] messageArgs){ super(cause); this.errorCode = errorCode; this.messageId = messageId; if (null != messageArgs){ this.messageArgs = messageArgs.clone(); } } /** * 用指定code和异常日志 message构造一个BaseException * * @param errorCode 异常编码 * @param messageId 异常信息资源ID * @param messageArgs 异常信息资源参数 */ public UnexBaseException(String errorCode, String messageId, Object[] messageArgs){ this.errorCode = errorCode; this.messageId = messageId; if (null != messageArgs){ this.messageArgs = messageArgs.clone(); } } getter...setter }
具体使用:
public static Date parseDate(String text,String pattern){ try{ return formatter.parse(text); }catch (ParseException e){ throw new UnexBaseException("ParseDate failed. [" + text + "]"); } }
javabean的操作
继承spring.beans中的beans类,完成自己的自定义操作,因为我的项目中大量使用 Dto 和Vo 和Po 的转化,所以,我需要 能将 Vo和Po进行转化的工具类
其中使用了反射:
用其方法应优先使用java.beans.PropertyDescriptor获取Method进行方法调用
PropertyDescriptor类
PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
1、getPropertyType(),获得属性的Class对象。
2、getReadMethod(),获得用于读取属性值的方法;getWriteMethod(),获得用于写入属性值的方法。
public class BeanCopyUtil extends BeanUtils{ public static void copyProps(Object source,Object target,String[] igonre){ Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); Object methodName = null; // Object targetValue=null; // 需复制字段 for (PropertyDescriptor targetPd : targetPds){ if (targetPd.getWriteMethod() != null){ PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null){// 判空 Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())){ readMethod.setAccessible(true); } Object value = readMethod.invoke(source); // Check whether the value is empty, only copy the properties which are not empty // targetValue=value; if (value != null){// 判空 Method writeMethod = targetPd.getWriteMethod(); methodName = writeMethod.getName(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())){ readMethod.setAccessible(true); } writeMethod.invoke(target, value); } } }}
就是 一个 将vo中的属性值全部利用反射注入到po中,
参考
Person po=new Person(12,"sdf"); PropertyDescriptor pd = new PropertyDescriptor("age",po.getClass()); Method methodGetX = pd.getReadMethod();//Read对应get()方法 Object reValue = methodGetX.invoke(po); System.out.println(reValue);
age— 这个东西
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
是springBean中的提供的获取所有的属性的方法,
枚举类工具类
通常我们会遇到 根据 code 获取到 整个Enum,或者 根据 Msg 获取到整个 Enum,
public class UnexEnumUtil { public static <E extends Enum<?>> E code2Enum(Class<E> enumClass, String code) { // Only for IUnexEnum if (!IUnexEnum.class.isAssignableFrom(enumClass)) { return null; } for(E e : enumClass.getEnumConstants()) { IUnexEnum<?> ce = IUnexEnum.class.cast(e); if (ce.equals(code)) { return e; } } return null; } }
Po类公共属性
public class UnexBaseVo implements java.io.Serializable { // serialVersionUID private static final long serialVersionUID = 1566887093391005767L; // ID private Long id; // 创建者 private Long createUser; // 创建时间 private Date createTime; // 更新者 private Long updateUser; // 更新时间 private Date updateTime; // 逻辑删除: 0-未删除 1-已删除 private String logicDelete; getter .setter...}
阅读全文
0 0
- 微服务(一)--Common
- 微服务(三)---Common
- 微服务(一)
- 微服务指南走北(一):微服务是什么
- 微服务架构学习笔记(一)
- SpringBoot搭建微服务(一)HelloWorld!
- 我也来谈微服务(一)
- 微服务时代---Dubbo(一)
- 微服务设计笔记(一)
- 微服务(四)--dao层一
- 读书笔记《微服务一》
- 什么是微服务?(一)
- 一、什么是微服务
- 微服务架构(一):什么是微服务
- 微服务之微服务架构的优势与不足(一)
- 微服务从设计到部署(一)微服务简介
- 从0开始的微服务架构:(一)重识微服务架构
- 都在说微服务,那么微服务的反模式和陷井是什么(一)
- 常用事件分类
- el表达式动态获取map的key
- 年度大盘点:那些最值得你了解的容器技术
- Redis 客户端Jedis 事务
- Linux 内核编译
- 微服务(一)--Common
- 【Java】10.CyclicBarrier使用与分析
- swift 使用AFNetWorking 进行网络请求
- 汉诺塔游戏-慕课网python入门理解
- 【JavaEE学习笔记】Oracle_02_序列,分页,常用函数,jdbc,PL/SQL
- mysql不存在mysql.sock
- log4j文件的配置基于java框架
- 凯哥自媒体:要不要辞职做自媒体?看过本文再做决定!
- springboot整合shiro-登录认证和权限管理