NotWritablePropertyException错误

问题遇到的现象和发生背景

发现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?

类重名,

img

img

img


ioc配置文件配置的是这个同名测试类

你这两个类都是Person,配置文件里定义对象的类是第二个Person类,第二个Person类没有setAge方法,所以不能完成age属性的赋值。把配置文件里的bean标签的class属性换成第一个Person类的全限定类名。