【问题标题】:Exception org.springframework.beans.NotWritablePropertyException caused but i don't know why导致异常 org.springframework.beans.NotWritablePropertyException 但我不知道为什么
【发布时间】:2019-04-07 18:14:50
【问题描述】:

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

线程“main”中的异常 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;
    }

}

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

【问题讨论】:

    标签: spring


    【解决方案1】:

    看看你的spring配置helloGeorgia应该是helloWorld bean的孩子。在Java类中并非如此。要么你也应该在java中继承,或者,至少;您应该在 helloGeorgia 中使用 getter/setter 声明相同的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多