Java PropertyUtils.getProperty在类中不抛出getter方法
正如您所看到的,在类TemplateVars中,我显然有一个属性“name”的getter。一段类似的代码在系统的其他地方工作。那么为什么下面的代码会引发下面的异常呢 代码Java PropertyUtils.getProperty在类中不抛出getter方法,java,apache-commons-beanutils,Java,Apache Commons Beanutils,正如您所看到的,在类TemplateVars中,我显然有一个属性“name”的getter。一段类似的代码在系统的其他地方工作。那么为什么下面的代码会引发下面的异常呢 代码 public class Main { public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetExcep
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)
我上面发布的这段代码只是作为概念证明。这仅仅是因为您的类
TemplateVars
和Contact
具有包本地访问修饰符,而PropertyUtils
无法访问它们
为了解决这个问题,让它们成为顶级类(即public),或者是
Main
类的公共静态内部类。为了概念证明,请使用一个真正的:a)没有不需要的内容(比如那里的模式匹配)和B)一些易于编译和执行的东西。@GhostCat据我所知,这是一个MCVE。它肯定不是最小的。再次说明:模式匹配与此有什么关系?结果证明,即使是静态内部类也必须是公共的。我找不到这方面的文档behavior@defaultlocale当然,我是认真的,但忘了提。谢谢,我已经更新了我的例子,我有一个带有公共访问修饰符的类,然后我也得到了这个错误