当前位置:  开发笔记 > 编程语言 > 正文

Java PropertyUtils.getProperty在类中不引发getter方法

如何解决《JavaPropertyUtils.getProperty在类中不引发getter方法》经验,为你挑选了1个好方法。

正如你所看到的,我在TemplateVars类中明显有一个属性'name'的getter.类似的代码片段在系统的其他地方起作用.那么为什么下面的代码抛出以下异常呢?

public class Main {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        String template = "Hello. My name is %%name%% %%surname%% %%contact.email%%";
        Pattern pattern = Pattern.compile("%%(.*?)%%");
        Matcher matcher = pattern.matcher(template);

        TemplateVars item = new TemplateVars();

        while (matcher.find()) {
            String placeHolder = matcher.group(1);
            String value;

            if(placeHolder.contains(".")){
                value = PropertyUtils.getNestedProperty(item, placeHolder).toString();
            }else{
                value = PropertyUtils.getProperty(item,placeHolder).toString();
            }
            template = template.replace("%%" + placeHolder + "%%", value);
        }

        System.out.println(template);
    }

}

class TemplateVars {
    private String name = "Boo";
    private String surname = "Foo";
    private Contact contact;

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }
}

class Contact {
    private String email = "boo.foo@mail.com";

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

例外

Exception in thread "main" java.lang.NoSuchMethodException: Property 'name' has no getter method in class 'class TemplateVars'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1274)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:808)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:884)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464)
    at Main.main(Main.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我上面发布的这段代码只是作为概念证明.



1> Andremoniy..:

这完全是因为你的类TemplateVarsContact具有包本地访问修饰符和PropertyUtils不能访问它们.

要解决它,使它们成为顶级类(即公共类)或类的公共静态内部Main类.

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有