MutablePropertyValues的简单用法


代码例子:

 1 package com.cy.model;
 2 
 3 import lombok.Getter;
 4 import lombok.Setter;
 5 import lombok.ToString;
 6 
 7 @Getter
 8 @Setter
 9 @ToString
10 public class Apple {
11     private String id;
12     private String name;
13 }
View Code


MutablePropertyValues的使用:

 1 package com.cy.test.spring;
 2 
 3 import com.cy.model.Apple;
 4 import org.springframework.beans.BeanWrapper;
 5 import org.springframework.beans.MutablePropertyValues;
 6 import org.springframework.beans.PropertyAccessorFactory;
 7 
 8 public class TestMutablePropertyValues {
 9 
10     public static void main(String[] args) {
11         //Apple的属性名称
12         String[] properties = new String[]{"id", "name"};
13         MutablePropertyValues pvs = new MutablePropertyValues();
14 
15         for(int i=0; i<properties.length; i++){
16             String property = properties[i];
17             String value = "test" + i;
18             pvs.add(property, value);
19         }
20 
21         Apple p = new Apple();
22         BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(p);
23         wrapper.setPropertyValues(pvs);
24         System.out.println(p);
25     }
26 }

打印结果:

Apple(id=test0, name=test1)

注:

1.MutablePropertyValues中设置的属性值类型,要和Apple中属性对应的值类型要匹配,不然会报错。java.lang.NumberFormatException等等。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM