java 使用 BeanUtils 设置 setter 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14867658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:41:08  来源:igfitidea点击:

Use BeanUtils for Setting setter value

javareflectionapache-commons-beanutils

提问by sunleo

I try to set value using setter but null comes.Please help me with this and give if some other better way is there to do.

我尝试使用 setter 设置值,但 null 来了。请帮我解决这个问题,并给出是否有其他更好的方法可以做。

import org.apache.commons.beanutils.BeanUtils;

public class TestSetter {

    public static void main(String args[]) throws Exception
    {
        Test t = new Test();
        BeanUtils.setProperty(t,"te","teval");
        System.out.println("tevalue :"+t.getTe());
    }
}
class Test
{
    String te;

    public String getTe() {
        return te;
    }

    public void setTe(String te) {
        this.te = te;
    }

}

Exception :

例外 :

Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set te
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1025)
    at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:313)
    at test.reflection.TestSetter.main(TestSetter.java:10)
Caused by: java.lang.NoSuchMethodException: Property 'te' has no setter method
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1746)
    at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
    at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
    ... 2 more

回答by Jayamohan

Your class Testshould be a publicclass , Move Testto a own file, make it public and rerun your code.

你的班级Test应该是一个公共班级,移动Test到一个自己的文件,公开并重新运行你的代码。

回答by Shawn

Set it to the name of the field:

将其设置为字段名称:

BeanUtils.setProperty(t,"te","teval");

Documentationhere

文档在这里

回答by NPKR

The method signature of setProperty()

方法签名 setProperty()

public static void setProperty(Object bean,
                               String name,
                               Object value)
                        throws IllegalAccessException,
                               InvocationTargetException

    Parameters:
        bean - Bean on which setting is to be performed
        name - Property name (can be nested/indexed/mapped/combo)
        value - Value to be set 

name is Property name "te" not "setTe".

name 是属性名称“te”而不是“setTe”。

BeanUtils.setProperty(t,"te","teval");

BeanUtils.setProperty(t,"te","teval");

回答by Andrew_chy

The class must be public, and provide a public constructor that accepts no arguments. This allows tools and applications to dynamically create new instances of your bean, without necessarily knowing what Java class name will be used ahead of time, like this:

该类必须是public,并提供一个不接受任何参数的公共构造函数。这允许工具和应用程序动态创建 bean 的新实例,而不必提前知道将使用什么 Java 类名,如下所示:

     String className = ...;
     Class beanClass = Class.forName(className);
     Object beanInstance = beanClass.newInstance();

get from http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#FAQ.property

http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#FAQ.property获取