Spring Expression Language (SpEL) Description and Hello World example

Source: Internet
Author: User

Spring Expression Language (SpEL) Description and Hello World example
As one of the basic modules of spring, spring Expression Language (SpEL) provides a powerful Expression Language for querying and operating an object graph at runtime. Spring EL can be configured either through XML or annotations. The following uses a simple example to demonstrate how to configure SpEL in two different ways to inject String, integer, and javabean data. The Spring EL dependency project is managed through maven. The core dependency of spring is declared in the pom. xml configuration file of maven. maven will automatically download the dependency package. (If maven is not used for project management, you can directly download and add the jar package to buildpath)

pom.xml:<properties><spring.version>3.1.4.RELEASE</spring.version></properties><dependencies><!-- 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>

 

2. Spring Beans define two simple Beans. Then, SpEL is used to inject values into bean attributes in XML and annotation modes.
public class Customer {    private Item item;    private String itemName;}public class Item {    private String name;    private int qty;}

 

3. Configure the Spring ELSpEL expression format in XML format as follows # {SpEL expression}. Configure SpEL in the XML bean definition file, for example:
<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="com.mkyong.core.Item">        <property name="name" value="itemA" />        <property name="qty" value="10" />    </bean>    <bean id="customerBean" class="com.mkyong.core.Customer">        <property name="item" value="#{itemBean}" />        <property name="itemName" value="#{itemBean.name}" />    </bean></beans>

 

# {ItemBean}-inject itemBean into the item attribute of customerBean # {itemBean. name}-inject the name attribute of itemBean into the itemName attribute of customerBean. 4. configure Spring EL through annotation Note: to configure SpEL through annotation, you must register the bean through annotation. If the bean is registered in the XML configuration file and @ Value is defined in the java class, @ Value will become invalid. The annotation configuration bean is as follows:
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;    //...}          import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component("itemBean")public class Item {    @Value("itemA") //inject String directly    private String name;    @Value("10") //inject interger directly    private int qty;    public String getName() {        return name;    }    //...}

 

Allow automatic scanning in the configuration 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-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:component-scan/></beans>

 

In the annotation method, use @ Value to define springEL. Through the preceding configuration, the string and integer values are directly injected into the itemBean attributes, and the itemBean is then injected into the customerBean attributes. 5. Output and run the following code. Both the XML method and the annotation method will output the same result.
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);    }}

 

Output: 1 Customer [item = Item [name = itemA, qty = 10], itemName = itemA] similar to jsf el. In addition to setting values directly, SpEL also supports multiple expressions, including relational expressions, regular expressions, arrays, and lists.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.