• 使用commons-beanutils向java 类动态添加属性


    1. <!--使用cglib 为javabean动态添加属性-->
    2. <dependency>
    3. <groupId>commons-beanutils</groupId>
    4. <artifactId>commons-beanutils</artifactId>
    5. <version>1.9.3</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>com.alibaba</groupId>
    9. <artifactId>fastjson</artifactId>
    10. <version> 1.2.54</version>
    11. </dependency>
    1. package com.example.cloudprovider.api;
    2. public class Apple {
    3. private int height;
    4. private int price;
    5. private int money;
    6. public Apple() {
    7. }
    8. public Apple(int height, int price){
    9. this.height = height;
    10. this.price = price;
    11. this.money = height*price;
    12. }
    13. public int getHeight() {
    14. return height;
    15. }
    16. public void setHeight(int height) {
    17. this.height = height;
    18. }
    19. public int getPrice() {
    20. return price;
    21. }
    22. public void setPrice(int price) {
    23. this.price = price;
    24. }
    25. public int getMoney() {
    26. return getHeight()*getPrice();
    27. }
    28. public void setMoney(int money) {
    29. this.money = getHeight()*getPrice();
    30. }
    31. }
    1. package com.example.cloudprovider.api;
    2. import com.google.common.collect.Maps;
    3. import org.apache.commons.beanutils.PropertyUtilsBean;
    4. import java.beans.PropertyDescriptor;
    5. import java.util.Map;
    6. public class PicBeanAddPropertiesUtil {
    7. public static Object getTarget(Object dest, Map<String, Object> addProperties) {
    8. // get property map
    9. PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
    10. PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
    11. Map<String, Class> propertyMap = Maps.newHashMap();
    12. for (PropertyDescriptor d : descriptors) {
    13. if (!"class".equalsIgnoreCase(d.getName())) {
    14. propertyMap.put(d.getName(), d.getPropertyType());
    15. }
    16. }
    17. // add extra properties
    18. addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
    19. // new dynamic bean
    20. DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
    21. // add old value
    22. propertyMap.forEach((k, v) -> {
    23. try {
    24. // filter extra properties
    25. if (!addProperties.containsKey(k)) {
    26. dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
    27. }
    28. } catch (Exception e) {
    29. e.printStackTrace();
    30. }
    31. });
    32. // add extra value
    33. addProperties.forEach((k, v) -> {
    34. try {
    35. dynamicBean.setValue(k, v);
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }
    39. });
    40. Object target = dynamicBean.getTarget();
    41. return target;
    42. }
    43. }

    1. package com.example.cloudprovider.api;
    2. import com.alibaba.fastjson.JSON;
    3. import org.springframework.cglib.beans.BeanGenerator;
    4. import org.springframework.cglib.beans.BeanMap;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. public class DynamicBean {
    8. /**
    9. * 目标对象
    10. */
    11. private Object target;
    12. /**
    13. * 属性集合
    14. */
    15. private BeanMap beanMap;
    16. public DynamicBean(Class superclass, Map<String, Class> propertyMap) {
    17. this.target = generateBean(superclass, propertyMap);
    18. this.beanMap = BeanMap.create(this.target);
    19. }
    20. /**
    21. * bean 添加属性和值
    22. *
    23. * @param property
    24. * @param value
    25. */
    26. public void setValue(String property, Object value) {
    27. beanMap.put(property, value);
    28. }
    29. /**
    30. * 获取属性值
    31. *
    32. * @param property
    33. * @return
    34. */
    35. public Object getValue(String property) {
    36. return beanMap.get(property);
    37. }
    38. /**
    39. * 获取对象
    40. *
    41. * @return
    42. */
    43. public Object getTarget() {
    44. return this.target;
    45. }
    46. /**
    47. * 根据属性生成对象
    48. *
    49. * @param superclass
    50. * @param propertyMap
    51. * @return
    52. */
    53. private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
    54. BeanGenerator generator = new BeanGenerator();
    55. if (null != superclass) {
    56. generator.setSuperclass(superclass);
    57. }
    58. BeanGenerator.addProperties(generator, propertyMap);
    59. return generator.create();
    60. }
    61. public static void main(String[] args) {
    62. Apple entity = new Apple();
    63. entity.setHeight(111);
    64. entity.setMoney(222);
    65. Map<String, Object> addProperties = new HashMap() {{
    66. put("name", "动态属性值");
    67. put("field1", "摇一摇送金币");
    68. put("field2", "参与次数");
    69. }};
    70. Apple finalPicBaseReqVo = (Apple) PicBeanAddPropertiesUtil.getTarget(entity, addProperties);
    71. System.out.println(JSON.toJSONString(finalPicBaseReqVo));
    72. }
    73. }

    输出:

    {"field1":"摇一摇送金币","field2":"参与次数","height":111,"money":0,"name":"动态属性值","price":0}

  • 相关阅读:
    预防山体滑坡,泥石流监测智能预警系统
    【ELM回归预测】探路者优化极限学习机回归预测【含Matlab源码 2231期】
    java解压zip压缩包
    CSS学习273~297(HTML5和CSS新特性)
    mysql约束
    互联网大厂java面试题一美团
    LetCode算法题
    qps、tps、吞吐量
    (附源码)node.js宠物医生预约平台 毕业设计 030945
    swift使用swift-protobuf协议通讯,使用指北
  • 原文地址:https://blog.csdn.net/weixin_33387378/article/details/127763985