Spring问题 - NotWritablePropertyException

时间:2011-03-31 03:40:54

标签: java spring

我有一个从教科书中获取的以下Spring代码,它返回一个NotWritablePropertyException。我被告知我需要为我的Song课程设置,我认为我刚刚完成,但它显然不起作用。我在下面编辑了我的代码。有人可以帮忙吗?这是代码。

package com.springinaction.springidol;

    public class Singer implements Performer {
        private String name = "Someone";
        private Song song;

        public Singer() {
        }

        public Singer(String song) {
            this.song.setTitle(song);
        }

        public Singer(String name, String song) {
            this.song = new Song(song);
            this.name = name;
        }

        public Song getSong() {
            return this.song;
        }

        public void setSong(Song song) {
            this.song = song;
        }

        public void perform() throws PerformanceException {
            System.out.println(name + " IS SINGING " + song.getTitle());
        }
    }

这是我的Song课程,似乎是问题的一部分。

public class Song {
    private String title;

    public Song(String song) {
        this.title = song;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

以下是例外情况。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taylor' defined in class path resource [com/springinaction/springidol/spring-idol.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.springinaction.springidol.SpringIdolMain.main(SpringIdolMain.java:8)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    ... 14 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
    ... 18 more

4 个答案:

答案 0 :(得分:1)

在实践中,你应该为每个领域设置getter和setter,除非你因为某个原因而忽略它们。

答案 1 :(得分:1)

如果没有你的Spring配置,有点难以确定,但是看看代码和堆栈跟踪,看来你正在进行基于setter的注入,但没有提供任何getter和setter方法。所以你可以a)切换到使用基于构造函数的注入或b)为你想通过Spring设置的所有属性提供getter和setter。

答案 2 :(得分:1)

NotWritablePropertyException:尝试设置不可写的属性的值时抛出异常,因为没有setter方法。你检查该异常引发的类的所有属性并做getter &安培; setter方法

答案 3 :(得分:0)

来自NotWritablePropertyException

的JavaDoc
  

尝试设置不可写的属性的值时抛出异常,因为没有setter方法。在某些情况下,会有替代方案。

你没有制定者或吸气剂。