Java Spring问题--NotWritablePropertyException
我有下面的Spring代码,取自教科书,它返回一个NotWritablePropertyException。有人告诉我,我的歌曲课需要二传手,我以为我刚做了,但显然不行。我在下面编辑了我的代码。有人能帮忙吗?这是代码Java Spring问题--NotWritablePropertyException,java,spring,Java,Spring,我有下面的Spring代码,取自教科书,它返回一个NotWritablePropertyException。有人告诉我,我的歌曲课需要二传手,我以为我刚做了,但显然不行。我在下面编辑了我的代码。有人能帮忙吗?这是代码 package com.springinaction.springidol; public class Singer implements Performer { private String name = "Someone"; priva
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());
}
}
这是我的歌曲课,这似乎是问题的一部分
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
线程“main”org.springframework.beans.factory.BeanCreationException中的异常:创建在类路径资源[com/springinaction/springidol/springidol.xml]中定义的名为“taylor”的bean时出错:bean初始化失败;嵌套异常为org.springframework.beans.typemischException:未能将[java.lang.String]类型的属性值转换为属性“Song”所需的类型[com.springinaction.springidol.Song];嵌套异常为java.lang.IllegalArgumentException:无法将[java.lang.String]类型的值转换为属性“Song”所需的[com.springinaction.springidol.Song]类型:未找到匹配的编辑器或转换策略
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
位于java.security.AccessController.doPrivileged(本机方法)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:429)
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
位于com.springinaction.springidol.springidomain.main(springidomain.java:8)
原因:org.springframework.beans.typemischException:未能将[java.lang.String]类型的属性值转换为属性“Song”所需的类型[com.springinaction.springidol.Song];嵌套异常为java.lang.IllegalArgumentException:无法将[java.lang.String]类型的值转换为属性“Song”所需的[com.springinaction.springidol.Song]类型:未找到匹配的编辑器或转换策略
位于org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
... 14多
原因:java.lang.IllegalArgumentException:无法将[java.lang.String]类型的值转换为属性“Song”所需的[com.springinaction.springidol.Song]类型:未找到匹配的编辑器或转换策略
在org.springframework.beans.TypeConverterDelegate.ConvertifEssential上(TypeConverterDelegate.java:231)
在org.springframework.beans.TypeConverterDelegate.ConvertifEssential上(TypeConverterDelegate.java:138)
位于org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
... 还有18个
在实践中,每个字段都应该有getter和setter,除非您出于某种原因省略它们。在实践中,每个字段都应该有getter和setter,除非您出于某种原因省略它们。如果没有Spring配置,这里很难确定,但从代码和堆栈跟踪来看,似乎您正在进行基于setter的注入,但没有提供任何getter和setter方法。因此,您可以a)切换到使用基于构造函数的注入,或者b)为所有要通过Spring设置的属性提供getter和setter。如果没有Spring配置,这里很难确定,但从代码和堆栈跟踪来看,似乎您正在进行基于setter的注入,但没有提供任何getter和setter方法。因此,您可以a)切换到使用基于构造函数的注入,或者b)为您希望通过Spring设置的所有属性提供getter和setter。来自JavaDoc forNotWritablePropertyException
尝试设置不可写属性的值时引发异常,因为没有setter方法。在某些情况下,提出了备选方案
您没有setter或getter。来自
NotWritablePropertyException的JavaDoc
尝试设置不可写属性的值时引发异常,因为没有setter方法。在某些情况下,交替