Eclipse使用BeanUtils框架,给属性赋值,已导入commons-beanutils和commons-logging的jar包,使用BeanUtils.setProperty时提示java.lang.IllegalAccessException
Person类:
package day_220322;
public class Person {
public String name;
private String password;
private int age;
public Person() {
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
BeanDemo类:
package day_220322;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
public class BeanDemo {
@Test
public void test1() throws IllegalAccessException, InvocationTargetException {
Person p =new Person();
BeanUtils.setProperty(p, "name", "xcc");
// BeanUtils.setProperty(p, "age", 18);
System.out.println(p.getName());
}
}
运行Run as Junit时,提示:
java.lang.IllegalAccessException: class org.apache.commons.beanutils.PropertyUtilsBean (in module commons.beanutils) cannot access class day_220322.Person (in module JavaStudy) because module JavaStudy does not export day_220322 to module commons.beanutils
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:385)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:693)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2151)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1957)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2064)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at JavaStudy/day_220322.BeanDemo.test1(BeanDemo.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at junit@1500/org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at junit@1500/org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at junit@1500/org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at junit@1500/org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at junit@1500/org.junit.runners.ParentRunner$3.evaluat