- <!--使用cglib 为javabean动态添加属性-->
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- <version>1.9.3</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version> 1.2.54</version>
- </dependency>
- package com.example.cloudprovider.api;
-
-
- public class Apple {
- private int height;
- private int price;
- private int money;
-
- public Apple() {
- }
-
- public Apple(int height, int price){
- this.height = height;
- this.price = price;
- this.money = height*price;
- }
-
- public int getHeight() {
- return height;
- }
-
- public void setHeight(int height) {
- this.height = height;
- }
-
- public int getPrice() {
- return price;
- }
-
- public void setPrice(int price) {
- this.price = price;
- }
-
- public int getMoney() {
- return getHeight()*getPrice();
- }
-
- public void setMoney(int money) {
- this.money = getHeight()*getPrice();
- }
- }
- package com.example.cloudprovider.api;
-
- import com.google.common.collect.Maps;
- import org.apache.commons.beanutils.PropertyUtilsBean;
-
- import java.beans.PropertyDescriptor;
- import java.util.Map;
-
-
- public class PicBeanAddPropertiesUtil {
- public static Object getTarget(Object dest, Map<String, Object> addProperties) {
- // get property map
- PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
- PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
- Map<String, Class> propertyMap = Maps.newHashMap();
- for (PropertyDescriptor d : descriptors) {
- if (!"class".equalsIgnoreCase(d.getName())) {
- propertyMap.put(d.getName(), d.getPropertyType());
- }
- }
- // add extra properties
- addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
- // new dynamic bean
- DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
- // add old value
- propertyMap.forEach((k, v) -> {
- try {
- // filter extra properties
- if (!addProperties.containsKey(k)) {
- dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- // add extra value
- addProperties.forEach((k, v) -> {
- try {
- dynamicBean.setValue(k, v);
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- Object target = dynamicBean.getTarget();
- return target;
- }
- }
- package com.example.cloudprovider.api;
-
- import com.alibaba.fastjson.JSON;
- import org.springframework.cglib.beans.BeanGenerator;
- import org.springframework.cglib.beans.BeanMap;
-
- import java.util.HashMap;
- import java.util.Map;
-
-
- public 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();
- }
-
- public static void main(String[] args) {
- Apple entity = new Apple();
- entity.setHeight(111);
- entity.setMoney(222);
- Map<String, Object> addProperties = new HashMap() {{
- put("name", "动态属性值");
- put("field1", "摇一摇送金币");
- put("field2", "参与次数");
- }};
- Apple finalPicBaseReqVo = (Apple) PicBeanAddPropertiesUtil.getTarget(entity, addProperties);
- System.out.println(JSON.toJSONString(finalPicBaseReqVo));
- }
- }
输出:
{"field1":"摇一摇送金币","field2":"参与次数","height":111,"money":0,"name":"动态属性值","price":0}