tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Expression Language > XML Configuration

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.

File Name  :  
/SpringCore001/conf/spel/basic.xml 
01<?xml version="1.0" encoding="UTF-8"?>
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04       xsi:schemaLocation="http://www.springframework.org/schema/beans
06 
07    <bean id="rand" class="com.bethecoder.tutorials.spring3.basic.SimpleBean" scope="prototype">
08        <property name="simpleProp" value="#{ T(java.lang.Math).random() * 1729 }" />
09    </bean>
10 
11    <bean id="lists" class="com.bethecoder.tutorials.spring3.basic.ComplexBean" scope="prototype">
12        <property name="simpleList" value="#{   {'one', 'two', 'three', 'four'}   }" />
13    </bean>
14     
15    <bean id="info" class="com.bethecoder.tutorials.spring3.basic.SimpleBean" scope="prototype">
16        <property name="simpleProp" value="The value of 2 power 3 is #{ 2 ^ 3 }" />
17    </bean>
18     
19</beans>

File Name  :  
com/bethecoder/tutorials/spring3/basic/SimpleBean.java 
   
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;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/spring3/basic/ComplexBean.java 
   
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;
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/spel/XMLConfigurationTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
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 = (SimpleBeanfactory.getBean("rand");
    System.out.println("Random Number : " + rand.getSimpleProp());

    ComplexBean lists = (ComplexBeanfactory.getBean("lists");
    System.out.println("String list : " + lists.getSimpleList());
    
    SimpleBean info = (SimpleBeanfactory.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



 
  


  
bl  br