我是靠谱客的博主 优美紫菜,最近开发中收集的这篇文章主要介绍动态给类添加属性pom文件DynamicBeanUtilsDynamicBeanUtils2TestBean,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ymqx</groupId>
<artifactId>MyTest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MyTest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
DynamicBeanUtils
package com.ymqx.动态给类添加属性;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
/*
* 思路:
* 1、通过commons-beanutils包下的类 PropertyUtilsBean 获取目标对象的属性
* 2、将需要新增的属性加入原对象的属性集合
* 3、通过cglib包下的类 BeanGenerator 创建新的Bean
* 4、遍历新创建的bean属性,为新增的属性赋值
*
*/
public class DynamicBeanUtils {
public static Object getTarget(Object dest, Map<String, Object> addProperties) {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
//得到原对象的属性
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
Map<String, Class<?>> propertyMap = new HashMap<>();
for (PropertyDescriptor d : descriptors) {
if (!"class".equalsIgnoreCase(d.getName())) {
propertyMap.put(d.getName(), d.getPropertyType());
}
}
addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
//构建新的对象
DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
propertyMap.forEach((k, v) -> {
try {
if (!addProperties.containsKey(k)) {
//原来的值
dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
}else {
//新增的值
dynamicBean.setValue(k, addProperties.get(k));
}
} catch (Exception e) {
e.printStackTrace();
}
});
return dynamicBean.getTarget();
}
private static class DynamicBean {
/**
* 目标对象
*/
private Object target;
/**
* 属性集合
*/
private BeanMap beanMap;
public DynamicBean(Class<?> superclass, Map<String, Class<?>> propertyMap) {
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
* @return
*/
private Object generateBean(Class<?> superclass, Map<String, Class<?>> propertyMap) {
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}
}
DynamicBeanUtils2
package com.ymqx.动态给类添加属性;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import java.util.HashMap;
import java.util.Map;
/*
* 思路:
* 1、通过cglib包下的类 beanMap 获取目标对象的属性
* 2、将需要新增的属性加入原对象的属性集合
* 3、通过cglib包下的类 BeanGenerator 创建新的Bean
* 4、遍历新创建的bean属性,为新增的属性赋值
*
*/
public class DynamicBeanUtils2 {
public static Object getTarget(Object dest, Map<String, Object> addProperties) {
// 获取原对象属性集合
BeanMap beanMap = BeanMap.create(dest);
Map<String, Class<?>> propertyMap = new HashMap<>();
beanMap.forEach((k, v) -> {
propertyMap.put(k.toString(), v.getClass());
});
addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
//构建新的对象
DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
propertyMap.forEach((k, v) -> {
try {
if (!addProperties.containsKey(k)) {
//原来的值
dynamicBean.setValue(k, beanMap.get(k));
}else {
//新增的值
dynamicBean.setValue(k, addProperties.get(k));
}
} catch (Exception e) {
e.printStackTrace();
}
});
return dynamicBean.getTarget();
}
private static class DynamicBean {
/**
* 目标对象
*/
private Object target;
/**
* 属性集合
*/
private BeanMap beanMap;
public DynamicBean(Class<?> superclass, Map<String, Class<?>> propertyMap) {
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
* @return
*/
private Object generateBean(Class<?> superclass, Map<String, Class<?>> propertyMap) {
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}
}
TestBean
package com.ymqx.动态给类添加属性;
import com.alibaba.fastjson.JSON;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
public class TestBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void printFields(Object obj) throws IllegalAccessException {
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field f : declaredFields) {
f.setAccessible(true);
System.out.println(f.getName() + "=" + f.get(obj));
}
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
TestBean bean = new TestBean();
bean.setName("张三");
printFields(bean);
System.out.println("User:"+ JSON.toJSONString(bean));
Map<String, Object> map = new HashMap<>();
map.put("age", 29);
//添加参数age--->29
Object obj = DynamicBeanUtils.getTarget(bean, map);
//打印结果
printFields(obj);
System.out.println("User:"+JSON.toJSONString(obj));
System.out.println("-----------------");
TestBean bean2 = new TestBean();
bean2.setName("张三");
printFields(bean2);
System.out.println("User:"+ JSON.toJSONString(bean2));
Map<String, Object> map2 = new HashMap<>();
map2.put("age", 29);
//添加参数age--->29
Object obj2 = DynamicBeanUtils2.getTarget(bean2, map2);
//打印结果
printFields(obj2);
System.out.println("User:"+JSON.toJSONString(obj2));
}
}
运行结果:
name=张三
User:{"name":"张三"}
$cglib_prop_name=张三
$cglib_prop_age=29
User:{"age":29,"name":"张三"}
-----------------
name=张三
User:{"name":"张三"}
$cglib_prop_name=张三
$cglib_prop_age=29
User:{"age":29,"name":"张三"}
动态的为实体字段添加注解/注解属性
https://blog.csdn.net/Adda_Chen/article/details/120869545
最后
以上就是优美紫菜为你收集整理的动态给类添加属性pom文件DynamicBeanUtilsDynamicBeanUtils2TestBean的全部内容,希望文章能够帮你解决动态给类添加属性pom文件DynamicBeanUtilsDynamicBeanUtils2TestBean所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复