我是春天的新人。我在书中做了一个例子 - " Spring in Action 2"。这个例子描述了如何编写bean并最终得到错误。请帮我解决这个问题。这是代码:
package com.my.quest;
public interface Instrument {
void play();
}
package com.my.quest;
public class Piano implements Instrument {
public Piano(){}
@Override
public void play() {
System.out.println("text");
}
}
package com.my.quest;
public interface Performer {
void perform ();
}
package com.my.quest;
public class Instrumentalist implements Performer {
private Instrument inst;
private String song;
public Instrumentalist() {
}
void setSong(String song){
this.song = song;
}
void setInstrument(Instrument inst){
this.inst = inst;
}
@Override
public void perform() {
System.out.println("Playing " + song);
inst.play();
}
}
bean名为 - config.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-2.0.xsd">
<bean id="kenny" class="com.my.quest.Instrumentalist">
<property name="song" value="Cage The Elephant – Shake Me Down" />
<property name="inst" ref="piano"/>
</bean>
<bean id="piano" class="com.my.quest.Piano">
</bean>
</beans>
我已经以这种方式进行了测试并且正在运行:
package com.my.quest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuestStart {
public static void main(String[] args) {
Instrument inst = new Piano();
Instrumentalist in = new Instrumentalist();
in.setInstrument(inst);
in.setSong("Cage The Elephant – Shake Me Down");
in.perform();
}
}
结果:在控制台中键入消息 - &gt;玩凯奇大象 - 震撼我:文字
但如果我使用它:
package com.my.quest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuestStart {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/my/quest/config.xml");
Performer p = (Performer)ctx.getBean("kenny");
p.perform();
}
}
我收到此错误:
фев 08, 2015 11:25:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7106e68e: startup date [Sun Feb 08 23:25:51 EET 2015]; root of context hierarchy
фев 08, 2015 11:25:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/my/quest/config.xml]
фев 08, 2015 11:25:52 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@256216b3: defining beans [kenny,piano]; root of factory hierarchy
фев 08, 2015 11:25:52 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@256216b3: defining beans [kenny,piano]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kenny' defined in class path resource [com/my/quest/config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'song' of bean class [com.my.quest.Instrumentalist]: Bean property 'song' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.my.quest.QuestStart.main(QuestStart.java:19)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'song' of bean class [com.my.quest.Instrumentalist]: Bean property 'song' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1031)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:899)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 13 more
下载了几个JAR:
帮助,请告诉我我做错了什么。
答案 0 :(得分:2)
setSong
bean的public
方法应为Instrumentalist
:
public void setSong(String song) {
通常也应该有匹配的getSong
方法。