导致异常org.springframework.beans.NotWritablePropertyException,但我不知道为什么

时间:2019-04-07 18:14:51

标签: spring

我在春天新来的。我学习,但是在学习过程中我出错了。 我为Spring使用基于XML的配置,这是编译错误:

  

线程“主”中的异常org.springframework.beans.factory.BeanCreationException:创建类路径资源[Beans.xml]中定义的名称为“ helloGeorgia”的bean时出错:设置属性值时出错;嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[com.tutorialspoint.HelloGeorgia]的无效属性“ werili”:Bean属性“ werili”不可写,或者具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?

但是我不明白为什么。

这是bean.xml文件:

<?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-3.0.xsd">

    <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
        <property name="message" value = "Hello World" />
        <property name="werili" value = "Hello Hello" />
    </bean>


    <bean id="helloGeorgia" class="com.tutorialspoint.HelloGeorgia" 
             parent="helloworld">
        <property name="message" value="Hello Georgia" />
    </bean>
</beans>

,这是三个Java文件: MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class MainApp {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld f = (HelloWorld) context.getBean("helloworld");
        System.out.println(f.getMessage());

        HelloGeorgia georgia = (HelloGeorgia) context.getBean("helloGeorgia");
        System.out.println(georgia.getMessage());

    }
}

HelloWorld.java

package com.tutorialspoint;


public class HelloWorld {
    private String message;
    private String werili;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getWerili() {
        return werili;
    }

    public void setWerili(String werili) {
        this.werili = werili;
    }
}

HelloGeorgia.java

package com.tutorialspoint;

public class HelloGeorgia {
    private String message;

    public String getMessage() {
        return message;

    }

    public void setMessage(String message) {
        this.message = message;
    }

}

请告诉我如何解决它。谢谢

1 个答案:

答案 0 :(得分:2)

查看您的春季配置helloGeorgia应该是helloWorld bean的子代。与java类中的情况不同。您也应该在Java中继承,或者至少要继承。您应该在helloGeorgia中使用getter / setter声明相同的字段。