我正在尝试使用spring的p命名空间连接属性。问题是我得到了“org.springframework.beans.factory.BeanDefinitionStoreException :( ...)cvc-complex-type.3.2.2:属性'p:instrument-ref'不允许出现在元素'bean'中。 “
请查看我的xml课程并告诉我我做错了什么。
performers.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="guitar" class="com.competition.Guitar"></bean>
<bean id="instrumentalist" class="com.competition.Instrumentalist"
p:instrument-ref="guitar" p:song="some song">
</bean>
</beans>
Instrumentalist.java:
package com.competition;
public class Instrumentalist implements Performer {
private Instrument instrument;
private String song;
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public void perform() {
System.out.println("Playing " + song);
instrument.play();
}
}
Guitar.java:
package com.competition;
public class Guitar implements Instrument {
public void play() {
System.out.println("play on guitar");
}
}