package com.safeluck.aaej.app.config; import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.util.Assert; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; public final class BeanUtil extends PropertyUtilsBean implements BeanFactoryPostProcessor { private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境 @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { BeanUtil.beanFactory = beanFactory; } @SuppressWarnings("unchecked") public static T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } public static T getBean(Class clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } /** * getParameterizedType方法慨述:获取父类的泛型类型
* 创 建 人:伍乾伦
* 创建时间:2015年6月24日 下午2:18:58
* 修 改 人:(修改了该文件,请填上修改人的名字)
* 修改日期:(请填上修改该文件时的日期)
* * @param clazz
* @param index
* @return Class */ @SuppressWarnings("unchecked") public static Class getParameterizedType(Class clazz, int index) { Type parameterizedType = clazz.getGenericSuperclass(); if (!(parameterizedType instanceof ParameterizedType)) { parameterizedType = clazz.getSuperclass().getGenericSuperclass(); } if (!(parameterizedType instanceof ParameterizedType)) { return null; } Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments(); if (actualTypeArguments == null || actualTypeArguments.length == 0) { return null; } if (actualTypeArguments[0] instanceof ParameterizedType) { parameterizedType = actualTypeArguments[0]; return (Class) ((ParameterizedType) parameterizedType).getRawType(); } return (Class) actualTypeArguments[0]; } /** * 概 述: (浅)拷贝对象属性到目标对象,忽略部分属性的拷贝
* 创 建 人:wuqianlun
* 创建时间:2016年4月5日下午7:25:53
* 修 改 人:wuqianlun
* 修改日期:2016年4月5日下午7:25:53
* * @param from * @param to * @param ignores */ public static void copyIgnoreProperties(Object from, Object to, String... ignores) { Assert.notNull(from, "Source must not be null"); Assert.notNull(to, "Target must not be null"); PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(to.getClass()); List ignoreList = Arrays.asList(ignores); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod == null || ignoreList.contains(writeMethod.getName())) { continue; } PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(from.getClass(), targetPd.getName()); if (sourcePd == null) { continue; } Method readMethod = sourcePd.getReadMethod(); if (!(readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType()))) { continue; } try { // 性能考虑? if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(from); // 空字符串,作为NULL值处理 if (value instanceof String && StringUtils.isBlank(value.toString())) { value = null; } if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(to, value); } catch (Throwable ex) { throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } /** * 概 述: (浅)拷贝对象属性到目标对象,仅仅拷贝设置的属性
* 创 建 人:wuqianlun
* 创建时间:2016年4月5日下午7:26:04
* 修 改 人:wuqianlun
* 修改日期:2016年4月5日下午7:26:04
* * @param from * @param to * @param includes */ public static void copyIncludeProperties(Object from, Object to, String... includes) { Assert.notNull(from, "Source must not be null"); Assert.notNull(to, "Target must not be null"); Assert.notNull(includes, "includeProperties must not be null"); if (includes.length == 0) { return; } PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(to.getClass()); List includeList = Arrays.asList(includes); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (!(writeMethod != null && includeList.contains(targetPd.getName()))) { continue; } PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(from.getClass(), targetPd.getName()); if (sourcePd == null) { continue; } Method readMethod = sourcePd.getReadMethod(); if (!(readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType()))) { continue; } try { // 性能考虑? if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(from); // 空字符串,作为NULL值处理 if (value instanceof String && StringUtils.isBlank(value.toString())) { value = null; } if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(to, value); } catch (Throwable ex) { throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } }