问题遇到的现象和发生背景
发现NotWritablePropertyException
public class Person {
private Integer age;
public Person() {
}
public Person(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) { this.age=age;}
@Override
public String toString() {
return "Person{" +
"age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person01" class="com.ww.Person">
<property name="age" value="21"></property>
</bean>
</beans>
package com.ww;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Person {
@Test
public void test1(){
ApplicationContext ioc =
new ClassPathXmlApplicationContext("ioc.xml");
Person bean = (Person) ioc.getBean("person01");
System.out.println(bean.getClass());
}
}
问题相关代码,请勿粘贴截图
感觉是xml连接不上Person
运行结果及报错内容
at com.ww.Person.test1(Person.java:11)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'age' of bean class [com.ww.Person]: Bean property 'age' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?