Java 反射 Bean 属性 API

发布于 2024-11-04 12:05:56 字数 326 浏览 0 评论 0原文

是否有任何标准方法来访问 Java Bean 属性,例如

class A {
   private String name;

   public void setName(String name){
       this.name = name;
   }

   public String getName(){
       return this.name;
   }

}

,我可以使用 Reflection API 访问此 java bean 属性名称,这样当我更改属性值时,当我设置和获取该属性的值时,会自动调用 getName 和 setName 方法

Is there any standard way to access Java Bean Property like

class A {
   private String name;

   public void setName(String name){
       this.name = name;
   }

   public String getName(){
       return this.name;
   }

}

So can I access this java bean property name using Reflection API so that when I change the value of property the methods of getName and setName are called automatically when I set and get values of that property

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

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

发布评论

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

评论(4

掩于岁月 2024-11-11 12:06:48

我尝试过 BeanUtils 以及 PropertyDescriptors,因为我没有传递给我的方法的类的信息。我什至不知道传递的属性的数据类型,因此设置值变得很困难。我知道 BeanUtils 应该自动为属性进行转换并设置它,但它没有为我保存数据。所以最后我只能靠获取田地了。这就是我所做的:

Field[] fields = className.getDeclaredFields();
for (int i=0; i<fields.length ;i++)
{
  String element = fields[i].getName();
  String propertyType = fields[i].getType().getName();
  fields[i].setAccessible(true);
  if(propertyType.equalsIgnoreCase("java.lang.Integer"))
    {
      fields[i].set(mypojoObj, Integer.parseInt(parameterValue));
    }
    else
    {
      fields[i].set(mypojoObj, parameterValue);
    }
 }

我做了一个类似的 switch-case 将所有属性类型转换为正确的类型。从页面获取时,request.getParameter(paramname) 始终返回 String,因此这种转换对我有用。任何更好的直接数据转换选项都会非常有帮助。

I have tried BeanUtils as well as PropertyDescriptors because I did not have the information on the class that was passed to my method. I did not even know the data types of the properties passed and so setting values became difficult. I know that BeanUtils should do the conversion automatically for the property and set it but it was not saving the data for me. So finally, I had to rely on getting the fields. Here is what I did:

Field[] fields = className.getDeclaredFields();
for (int i=0; i<fields.length ;i++)
{
  String element = fields[i].getName();
  String propertyType = fields[i].getType().getName();
  fields[i].setAccessible(true);
  if(propertyType.equalsIgnoreCase("java.lang.Integer"))
    {
      fields[i].set(mypojoObj, Integer.parseInt(parameterValue));
    }
    else
    {
      fields[i].set(mypojoObj, parameterValue);
    }
 }

I did a similar switch-case to convert all property types to correct types. When fetching from the page, request.getParameter(paramname) was always returning String so this conversion worked for me. Any better options for direct data conversion will be really helpful.

梦幻的味道 2024-11-11 12:06:46

使用 java.beans.PropertyDescriptor

在您的情况下:

A a = new A();
PropertyDescriptor nameProperty = new PropertyDescriptor("name", A.class);
nameProperty.getWriteMethod().invoke(a, "potato");
System.out.println(nameProperty.getReadMethod().invoke(a));

Use a java.beans.PropertyDescriptor

In your case:

A a = new A();
PropertyDescriptor nameProperty = new PropertyDescriptor("name", A.class);
nameProperty.getWriteMethod().invoke(a, "potato");
System.out.println(nameProperty.getReadMethod().invoke(a));
梦里梦着梦中梦 2024-11-11 12:06:44

你需要的是 BeanInfo / Introspector 机制(参见 Bozho 的回答)。但是,直接使用它是很糟糕的,因此您可以使用提供基于属性的访问的库之一。最著名的可能是 Apache Commons / BeanUtils (另一个是 Spring 的 BeanWrapper抽象)

示例代码:

A someBean = new A();

// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);

// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny"); 

What you need is the BeanInfo / Introspector mechanism (see Bozho's answer). However, it's hell to use this directly, so you can use one of the Libraries that offer property-based access. The best-known is probably Apache Commons / BeanUtils (another one is Spring's BeanWrapper abstraction)

Example code:

A someBean = new A();

// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);

// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny"); 
自控 2024-11-11 12:06:32

你的问题很不清楚,但如果我明白了:

是的。 java.beans 包具有所谓的 内省器。在那里您可以读取 bean 的属性。

BeanInfo info = Introspector.getBeanInfo(Bean.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();

您可以通过名称找到所需的 PropertyDescriptor,然后可以调用 getReadMethod().invoke(..)

You question is very unclear, but if I get it:

Yes. The java.beans package has the so called Introspector. There you can read the properties of a bean.

BeanInfo info = Introspector.getBeanInfo(Bean.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();

You can find the desired PropertyDescriptor by its name and you can call getReadMethod().invoke(..)

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