Monday 6 January 2014

Spring Expression Language using XML

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.



The expression language supports the following functionality
  • Literal expressions
  • Boolean and relational operators
  • Regular expressions
  • Class expressions
  • Accessing properties, arrays, lists, maps
  • Method invocation
  • Relational operators
  • Assignment
  • Calling constructors
  • Bean references
  • Array construction
  • Inline lists
  • Ternary operator
  • Variables
  • User defined functions
  • Collection projection
  • Collection selection
  • Templated expressions

Example: Spring EL in xml

 Here is the content for Customer.java

 package com.mahesh.training.spring;

public class Customer {


    private Person person;
    
    private String personName;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }
  
    @Override
    public String toString() {
        return "Customer [person=" + person + ", personName=" + personName + "]";
    }
  
}

Here is the content for Person.java

package com.mahesh.training.spring;

public class Person {

    String name;
    String address;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
   
    @Override
    public String toString() {
        return "Person [address=" + address + ", age=" + age + ", name=" + name + "]";
    }


}
 


Here is the Spring EL Configuration file beans-EL.xml

The SpEL are enclosed with #{ SpEL expression }, see following example in XML bean definition file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="customer" class="com.mahesh.training.spring.Customer">
        <property name="person" value="#{personBean}"></property>
        <property name="personName" value="#{personBean.name}"></property>

    </bean>

    <bean id="personBean" class="com.mahesh.training.spring.Person">
        <property name="name" value="mahesh" />
        <property name="address" value="address" />
        <property name="age" value="30" />
    </bean>

</beans>



Here is the content for App.java

package clientpack;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mahesh.training.spring.Customer;

public class IOCTest6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
                "beans-EL.xml");
      
        Customer cust = (Customer) context.getBean("customer");
        System.out.println(cust);
      
    }
}


OutPut

 Customer [person=Person [address=address, age=30, name=mahesh], personName=mahesh]


No comments:

Post a Comment