目录
一个完整的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...
}