如何使用 BeanUtils 内省获取 Java 对象的所有属性列表?

发布于 2024-07-25 04:01:28 字数 372 浏览 4 评论 0原文

我有一个获取 POJO 作为参数的方法。 现在我想以编程方式获取 POJO 的所有属性(因为我的代码在运行时可能不知道其中的所有属性是什么)并且还需要获取属性的值。 最后,我将形成 POJO 的字符串表示形式。

我可以使用 ToStringBuilder,但我想根据我的要求以特定的格式构建输出字符串。

在 Beanutils 中可以这样做吗!? 如果是,有指向方法名称的指针吗? 如果不是,我应该编写自己的反射代码吗?

I have method which gets a POJO as it's parameter. Now I want to programmatically get all the attributes of the POJO (because my code may not know what are all the attributes in it at run time) and need to get the values for the attributes also. Finally I'll form a string representation of the POJO.

I could use ToStringBuilder, but I want build my output string in certain format specific to my requirement.

Is it possible to do so in Beanutils !? If yes, any pointers to the method name? If no, should I write my own reflection code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-08-01 04:01:28

我知道这是一个老问题了,但我认为它对其他人可能有用。

我找到了使用此 LOC 的部分解决方案

Field [] attributes =  MyBeanClass.class.getDeclaredFields();

这是一个工作示例:

import java.lang.reflect.Field;

import org.apache.commons.beanutils.PropertyUtils;

public class ObjectWithSomeProperties {

    private String firstProperty;

    private String secondProperty;


    public String getFirstProperty() {
        return firstProperty;
    }

    public void setFirstProperty(String firstProperty) {
        this.firstProperty = firstProperty;
    }

    public String getSecondProperty() {
        return secondProperty;
    }

    public void setSecondProperty(String secondProperty) {
        this.secondProperty = secondProperty;
    }

    public static void main(String[] args) {

        ObjectWithSomeProperties object = new ObjectWithSomeProperties();

        // Load all fields in the class (private included)
        Field [] attributes =  object.getClass().getDeclaredFields();

        for (Field field : attributes) {
            // Dynamically read Attribute Name
            System.out.println("ATTRIBUTE NAME: " + field.getName());

            try {
                // Dynamically set Attribute Value
                PropertyUtils.setSimpleProperty(object, field.getName(), "A VALUE");
                System.out.println("ATTRIBUTE VALUE: " + PropertyUtils.getSimpleProperty(object, field.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

I know this is a year old question, but I think it can be useful for others.

I have found a partial solution using this LOC

Field [] attributes =  MyBeanClass.class.getDeclaredFields();

Here is a working example:

import java.lang.reflect.Field;

import org.apache.commons.beanutils.PropertyUtils;

public class ObjectWithSomeProperties {

    private String firstProperty;

    private String secondProperty;


    public String getFirstProperty() {
        return firstProperty;
    }

    public void setFirstProperty(String firstProperty) {
        this.firstProperty = firstProperty;
    }

    public String getSecondProperty() {
        return secondProperty;
    }

    public void setSecondProperty(String secondProperty) {
        this.secondProperty = secondProperty;
    }

    public static void main(String[] args) {

        ObjectWithSomeProperties object = new ObjectWithSomeProperties();

        // Load all fields in the class (private included)
        Field [] attributes =  object.getClass().getDeclaredFields();

        for (Field field : attributes) {
            // Dynamically read Attribute Name
            System.out.println("ATTRIBUTE NAME: " + field.getName());

            try {
                // Dynamically set Attribute Value
                PropertyUtils.setSimpleProperty(object, field.getName(), "A VALUE");
                System.out.println("ATTRIBUTE VALUE: " + PropertyUtils.getSimpleProperty(object, field.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
欢你一世 2024-08-01 04:01:28

您是否尝试过 ReflectionToStringBuilder? 看起来应该按照您所描述的进行。

Have you tried ReflectionToStringBuilder? It looks like is should do what you describe.

dawn曙光 2024-08-01 04:01:28

使用反射获取所有属性/变量(仅名称)。 现在使用 getProperty 方法获取该变量的值

get all properties/variables ( just the name ) using reflection. Now use getProperty method to get the value of that variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文