Das-Spring-problem — NotWritablePropertyException
Ich habe den folgenden Quellcode, genommen aus einem lehrbuch, dass die Rückgabe eines NotWritablePropertyException. Mir wurde gesagt ich brauche setter für meinen Song-Klasse, die ich dachte, ich hätte nur getan, aber es ist anscheinend nicht funktioniert. Ich habe bearbeitet meinen code unten. Kann jemand helfen? Hier ist der code.
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());
}
}
Hier ist mein Song Klasse, das scheint ein Teil des Problems.
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;
}
}
Hier sind die Ausnahmen.
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
Du musst angemeldet sein, um einen Kommentar abzugeben.
in der Praxis sollten Sie Getter und setter für jedes Feld, es sei denn, Sie werden unterlassen, Ihnen für einen Grund.
Irgendwie schwer zu bestimmen, hier ohne Spring-Konfiguration, aber ein Blick auf den code und den stack-trace, es scheint, dass du tust, Set-based-injection-aber nicht jede getter-und setter-Methoden. So können Sie entweder a) Wechseln Sie zur Verwendung von Konstruktor-basierten Injektion oder b) get-und Set-Methoden für alle Eigenschaften, die Sie festlegen möchten, die über den Frühling.
NotWritablePropertyException : Exception geworfen bei einem Versuch, den Wert einer Eigenschaft, die nicht beschreibbar, weil es keine setter-Methode.überprüfen Sie alle attribute der Klasse, die die Ausnahme provozieren und tun Getter & setter
Aus der JavaDoc für
NotWritablePropertyException
Ihnen haben keine setter oder Getter.
Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song'
Sieht aus wie Sie versuchen, zu injizieren eineString
, nicht einSong
. Vielleicht solltest du die post, die problematische xml.