目录

Java 用属性名称字符串获取属性对象

一、场景分析

java 中没有 python 一样的方法,通过属性名称直接获取属性值。

python 复制代码
getattr(obj, name[, default]) : 访问对象的属性。
getattr(student, 'name')

java 中有 Map, 可以实现类似功能,但是如果我们现在有一个对象,要通过Map的方式获取属性值,还得先将对象转成Map,这需要借助一些JSON工具。java 中能依赖的,就只有反射了。

java 复制代码
map.get("name")

java 原生的反射接口都太繁琐,而且还得捕获各种异常。借助 spring ReflectionUtils 工具可以帮我们快速地实现这种功能。

二、代码实现

假设有这么一个学生对象 Student,它关联一个课程对象 Course。

java 复制代码
package com.study.student.entity;
import lombok.Data;
@Data
public class Student {
    private String name;
    private Course course;
}
----------------------------------------
package com.study.student.entity;
import lombok.Data;
@Data
public class Course {
    private String name;
    private Integer credit;
}

通过属性名称获取属性对象:

java 复制代码
package com.study.utils;
import com.study.student.entity.Course;
import com.study.student.entity.Student;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
/**
 * 反射工具
 */
public class ReflectionUtil {
    /**
     * 获取目标对象 target.fieldName 的属性值
     * @param clazz 目标对象 Class 类型
     * @param fieldName 属性名
     * @param target 目标对象
     * @return 属性值
     */
    public static Object getValueByFieldName(Class<?> clazz, String fieldName, Object target) {
        // 从 clazz 中获取 fieldName 属性名对应的 Field 对象
        Field field = ReflectionUtils.findField(clazz, fieldName);
        // 判空 
        Assert.notNull(field, "field must not be null");
        // 设置访问权限
        ReflectionUtils.makeAccessible(field);
        // 反射获取
        return ReflectionUtils.getField(field, target);
    }

    public static void main(String[] args) {
        Student student = new Student();
            Course course = new Course();
            course.setName("高等数学");
            course.setCredit(10);
        student.setName("小林");
        student.setCourse(course);
        Course course1 = (Course) ReflectionUtil.getValueByFieldName(Student.class, "course", student);
        System.out.println(course1);
    }
}
输出:
Course(name=高等数学, credit=10)
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
南星沐1 小时前
Spring Boot 常用依赖介绍
java·前端·spring boot
代码不停1 小时前
Java中的异常
java·开发语言
何似在人间5752 小时前
多级缓存模型设计
java·jvm·redis·缓存
多云的夏天2 小时前
ubuntu24.04-MyEclipse的项目导入到 IDEA中
java·intellij-idea·myeclipse
Fanxt_Ja2 小时前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
Aphelios3802 小时前
TaskFlow开发日记 #1 - 原生JS实现智能Todo组件
java·开发语言·前端·javascript·ecmascript·todo
weixin_448771723 小时前
使用xml模板导出excel
xml·java·excel
烁3474 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐
魔道不误砍柴功4 小时前
2025年Java无服务器架构实战:AWS Lambda与Spring Cloud Function深度整合
java·架构·serverless
smileNicky4 小时前
SpringBoot系列之集成Redisson实现布隆过滤器
java·spring boot·redis·布隆过滤器