Springs - SPLessons

Spring Expression Language

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Expression Language

Spring Expression Language

shape Description

SPEL stands for Spring expression language will consist of many features such as evaluating logical and mathematical expressions. SPEL is same like OGNL and JSF. Spring expression language coding will be available through XML, Annotations. Following is an example which explains how to use expression language declarations.

shape Example

Following is an example where pom is the maven dependency support. pom.xml [xml]<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.core</groupId> <artifactId>Spring3Example</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3Example</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>[/xml] A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. App.java [java] import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "SpringBeans.xml"); Customer obj = (Customer) context.getBean("customerBean"); System.out.println(obj); } } [/java] The Application Context is spring's more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface. Customer.java [java]import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{itemBean}") private Item item; @Value("#{itemBean.name}") private String itemName; public Item getItem() { return item; } public void setItem(Item item) { this.item = item; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } @Override public String toString() { return "Customer [item=" + item + ", itemName=" + itemName + "]"; } }[/java] Here used annotations, annotations are used to reduce the code and no need to use web.xml files. Here performed SET and GET methods. Item.java [java]import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("itemBean") public class Item { @Value("itemA") private String name; @Value("10") private int qty; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } @Override public String toString() { return "Item [name=" + name + ", qty=" + qty + "]"; } }[/java] AppTest.java [java]import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } } [/java] SpringBeans.xml [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="itemBean" class="Item"> <property name="name" value="itemA" /> <property name="qty" value="10" /> </bean> <bean id="customerBean" class="Customer"> <property name="item" value="#{itemBean}" /> <property name="itemName" value="#{itemBean.name}" /> </bean> </beans>[/xml] Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases. When using XML-based configuration metadata, you use the 'id' or 'name' attributes to specify the bean identifier(s). The 'id' attribute allows you to specify exactly one id, and as it is a real XML element ID attribute, the XML parser is able to do some extra validation when other elements reference the id; as such, it is the preferred way to specify a bean id. However, the XML specification does limit the characters which are legal in XML IDs. This is usually not a constraint, but if you have a need to use one of these special XML characters, or want to introduce other aliases to the bean. Output When compile the code following is the result will be generated in the console. [java]Customer [item=Item [name=itemA, qty=10], itemName=itemA][/java]

Summary

shape Points

  • Spring Expression Language - SPEL provides String templating functionality.
  • Spring Expression Language - pom.xml is the the maven dependency file needs to be placed.