XML Configuration
The Spring Expression Language (SpEL ) is a simple and powerful expression language
which helps to query and manipulate objects at runtime.
We can use Spring Expression Language to set a constructor argument or property value.
package com.bethecoder.tutorials.spring3.basic;
public class SimpleBean {
private String simpleProp;
public String getSimpleProp () {
return simpleProp;
}
public void setSimpleProp ( String simpleProp ) {
this .simpleProp = simpleProp;
}
}
package com.bethecoder.tutorials.spring3.basic;
public class ComplexBean {
private java.util.Properties simpleProps;
private java.util.List simpleList;
private java.util.Map simpleMap;
private java.util.Set simpleSet;
public java.util.Properties getSimpleProps () {
return simpleProps;
}
public void setSimpleProps ( java.util.Properties simpleProps ) {
this .simpleProps = simpleProps;
}
public java.util.List getSimpleList () {
return simpleList;
}
public void setSimpleList ( java.util.List simpleList ) {
this .simpleList = simpleList;
}
public java.util.Map getSimpleMap () {
return simpleMap;
}
public void setSimpleMap ( java.util.Map simpleMap ) {
this .simpleMap = simpleMap;
}
public java.util.Set getSimpleSet () {
return simpleSet;
}
public void setSimpleSet ( java.util.Set simpleSet ) {
this .simpleSet = simpleSet;
}
}
package com.bethecoder.tutorials.spring3.tests.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bethecoder.tutorials.spring3.basic.ComplexBean;
import com.bethecoder.tutorials.spring3.basic.SimpleBean;
public class XMLConfigurationTest {
/**
* @param args
*/
public static void main ( String [] args ) {
ApplicationContext factory = new ClassPathXmlApplicationContext ( "basic.xml" ) ;
SimpleBean rand = ( SimpleBean ) factory.getBean ( "rand" ) ;
System.out.println ( "Random Number : " + rand.getSimpleProp ()) ;
ComplexBean lists = ( ComplexBean ) factory.getBean ( "lists" ) ;
System.out.println ( "String list : " + lists.getSimpleList ()) ;
SimpleBean info = ( SimpleBean ) factory.getBean ( "info" ) ;
System.out.println ( "Info : " + info.getSimpleProp ()) ;
}
}
It gives the following output,
Random Number : 1376.2272749811557
String list : [one, two, three, four]
Info : The value of 2 power 3 is 8