0
我在春季新。我從書中做出了一個例子 - 「行動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();
}
}
豆子被稱爲 - 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>
I`ve以這種方式測試,它的工作:
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();
}
}
結果:打字消息控制檯 - >播放籠大象 - 動搖我:文字
但如果我使用這個:
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[email protected]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.s[email protected]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.s[email protected]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
Dowloaded幾個JAR`s:
- org.springframework.context和context.support - 3.1.0.M2
- 組織.springframework.asm - 3.1.0.M2
- org.springframework.expression - 3.1.0.M2
- org.springframework.beans - 3.1.0.M2
- org.springframework.core - 3.1.0.M2
- org.apache.common.logging
幫助,告訴我我在做什麼請錯。
謝謝,它解決了1錯誤。添加了修飾符和getter,但另一個已經出現:bean類的無效屬性'inst'[com.my.quest.Instrumentalist]:Bean屬性'inst'不可寫或具有無效的setter方法。 – Anthony 2015-02-08 22:29:51
同樣的問題:沒有'公共'的'setInst'方法... – Reimeus 2015-02-08 22:35:53
Omg,抱歉的傢伙,我一點都不注意。我的setter名稱錯了:public void setInstrument vs setInst。感謝您的幫助。 – Anthony 2015-02-09 01:28:37