Song.java:
package
com.lnn.spel.conditional;
public class Song {
private String name = null;
public String
getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
}
Instrument.java:
package
com.lnn.spel.conditional;
public class Instrument {
private String name = null;
private Song song = null;
public String
getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public Song getSong()
{
return song;
}
public void setSong(Song
song) {
this.song = song;
}
}
Opera.java:
package
com.lnn.spel.conditional;
public class Opera {
private Instrument instrument;
public Instrument
getInstrument() {
return instrument;
}
public void
setInstrument(Instrument instrument) {
this.instrument = instrument;
}
}
conditional.xml:
<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-3.0.xsd">
<bean id="song"
class = "com.lnn.spel.conditional.Song">
<property name="name"
value="Jingle Bells..."/>
</bean>
<bean id="song2"
class = "com.lnn.spel.conditional.Song">
<property name="name"
value="#{song.name?:'Every Night in my Dreams...'}"/>
</bean>
<bean id="instrument1"
class="com.lnn.spel.conditional.Instrument">
<property name="name"
value="#{song.getName() =='Jingle Bells...'? 'piano' :
'violin'}"/>
<property name="song"
ref="song"/>
</bean>
<bean id="instrument2"
class="com.lnn.spel.conditional.Instrument">
<property name="name"
value="violin"/>
<property name="song"
value="#{song2}"/>
</bean>
<bean id="opera"
class = "com.lnn.spel.conditional.Opera">
<property name="instrument"
value="#{song.name =='Every Night in my Dreams...'?
instrument1 : instrument2}"/>
</bean>
</beans>
Test.java:
package
com.lnn.spel.conditional;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[]
args) {
ApplicationContext
context = new ClassPathXmlApplicationContext("conditional.xml");
Opera
opera = context.getBean("opera", Opera.class);
System.out.println(opera.getInstrument().getName());
System.out.println(opera.getInstrument().getSong().getName());
}
}
No comments:
Post a Comment