Java设计模式之外观模式示例详解
1556
2022-07-27
目录添加依赖代码测试
可以给已有实体类动态的添加字段并返回新的实体对象,不影响原来的实体对象结构。
添加依赖
代码
import com.google.common.collect.Maps;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.PropertyDescriptor;
import java.util.Map;
/**
* 动态为bean添加字段
* @Author gongl
* @Create 2022-01-11
*/
public class DynamicBeanUtils {
private static final Logger logger = LoggerFactory.getLogger(DynamicBeanUtils.class);
public static Object getTarget(Object dest, Map
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
//得到原对象的属性
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
Map
for (PropertyDescriptor d : descriptors) {
if (!"class".equalsIgnoreCase(d.http://getName())) {
propertyMap.put(d.getName(), d.getPropertyType());
}
}
addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
//构建新的对象
DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
for (Map.Entry
try {
if (!addProperties.containsKey(entry.getKey())) {//原来的值
dynamicBean.setValue(entry.getKey(), propertyUtilsBean.getNestedProperty(dest, entry.getKey()));
}else {//新增GukTADOguB的值
dynamicBean.setValue(entry.getKey(), addProperties.get(entry.getKey()));
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
return dynamicBean.getTarget();
}
private static class DynamicBean {
/**
* 目标对象
*/
private Object target;
/**
* 属性集合
*/
private BeanMap beanMap;
public DynamicBean(Class> superclass, Map
this.target = generateBean(superclass, propertyMap);
this.beanMap = BeanMap.create(this.target);
}
/**
* bean 添加属性和值
*
* @param property
* @param value
*/
public void setValue(String property, Object value) {
beanMap.put(property, value);
}
/**
* 获取属性值
*
* @param property
* @return
*/
public Object getValue(String property) {
return beanMap.get(property);
}
/**
* 获取对象
*
* @return
*/
public Object getTarget() {
return this.target;
}
/**
* 根据属性生成对象
*
* @param superclass
* @param propertyMap
http:// * @return
*/
private Object generateBean(Class> superclass, Map
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}
}
测试
public static class TestBean{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
TestBean bean = new TestBean();
bean.setName("张三");
Map
map.put("age", 29);
//添加参数age--->29
Object obj = DynamicBeanUtils.getTarget(bean, map);
//打印结果
Method[] declaredMethods = obj.getClass().getDeclaredMethods();
for(Method method:declaredMethods){
if(method.getName().startsWith("get")){
Object o=method.invoke(obj);
System.out.println("属性值get方法->"+o);
}
}
}
结果打印出name和age的值
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~