自定义BeanUtils的populate方法实现「建议收藏」

自定义BeanUtils的populate方法实现「建议收藏」1.1.1功能分析publicstaticvoidpopulate(Objectbean,Mapmap)//修改任意对象中的属性,为传入Map集合中的键和值思路:1.获取传入对象的字节码对象2.获取map集合中所有的键和值3.调用Class中的getDecl…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1.1.1 功能分析

public static void populate(Object bean,Map map)

                                // 修改任意对象中的属性, 为传入Map集合中的键和值

                思路:

1. 获取传入对象的字节码对象

2. 获取map集合中所有的键和值

3. 调用Class中的getDeclaredField()方法将每一个键传入, 得到Field对象

4. 通过Field对象中的set方法赋值

5. Try catch捕获getDeclaredField方法可能发生的异常.(为了方式传入错误的值)

1.1.2 实例代码

//public static void populate(Object bean,Map map)
 
public static void populate(Object bean,Map map) throws ReflectiveOperationException {
 
//通过JavaBean对象来获取对应的字节码对象
 
Class clazz = bean.getClass();
 
//获取Map中所有的key
 
Set keys = map.keySet();
 
for (Object key : keys) {
 
 
try {
 
//根据key来获取对应的Field对象
 
Field f = clazz.getDeclaredField(key.toString());
 
//根据key来获取Map中对应的value
 
Object value = map.get(key);
 
 
f.setAccessible(true);
 
f.set(bean, value);
 
} catch(NoSuchFieldException e) {
 
//e.printStackTrace();
 
}
 
}
 
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158753.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • # SpringCloud集成 报错 An attempt was made to call a method that does not exist. The attempt was made

    # SpringCloud集成 报错 An attempt was made to call a method that does not exist. The attempt was madeSpringCloud集成报错Anattemptwasmadetocallamethodthatdoesnotexist.Theattemptwasmadefromthefollowinglocation:详细报错结果如下:原因是SpringCloud和spring-boot-starter-parent的版本配置不搭配解决方法Greenwich2.1.x(可用2.1.4.RELEASE)Finchley2.0.x(可用2.0.5.RELEASE)

    2025年6月14日
    2
  • spring 中配置sessionFactory及用法

    spring 中配置sessionFactory及用法spring中配置sessionFactory及用法方法一:1、在Spring的applicationContext.xml中配置bean<!–启用注解注入–><c

    2022年7月2日
    28
  • CreateFile函数

    CreateFile函数在 include include 的头文件里 HANDLECreate LPCTSTRlpFil 要打开的文件名 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp DWORDdwDesir 文件的操作属性 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp DWORDdwShare 文件共享属性 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp LPSECU

    2025年9月25日
    3
  • python闭包详解_python闭包的使用场景

    python闭包详解_python闭包的使用场景闭包首先了解一下:如果在一个函数的内部定义了另一个函数,外部的我们叫他外函数,内部的我们叫他内函数。在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用

    2022年7月29日
    8
  • java单例模式案例,及单例体现的关键

    java单例模式案例,及单例体现的关键

    2021年7月16日
    74
  • axis2开发webservice_docker映射出来端口访问不了

    axis2开发webservice_docker映射出来端口访问不了记录一次正式环境服务报错排查记录。某日被通知线上服务告警,错入日志全是Timeoutwaitingforconnection首先梳理项目架构,项目很简单,就是一个使用axis2构建的webserice的客户端开始从此段报错入手排查,定位到MultiThreadedHttpConnectionManager这个类的doGetConnection方法privateHttpConnectiondoGetConnection(HostConfigurationhostCo.

    2025年11月3日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号