Powerful Spring EL expressions

Source: Internet
Author: User

Powerful Spring EL expressions

1. The first Spring EL example -- HelloWorld Demo

This example shows how to use SpEL to inject String, Integer, and Bean to attributes.
1) Dependency packages of Spring El

First, add the dependency package to the Maven pom. xml file to automatically download the SpEL dependency.
File: pom. xml


      
           
    
     org.springframework
            spring-core        
    
     3.2.4.RELEASE
        
       
           
    
     org.springframework
            spring-context        
    
     3.2.4.RELEASE
        
     
  

2) Spring Bean

Next, write two simple beans and inject value into the attributes using SpEL.
Item. java is as follows:

package com.spring.hello;public class Item {    private String name;    private int total;    //getter and setter...}

Customer. java is as follows:

package com.spring.hello;public class Customer {    private Item item;    private String itemName;  @Override    public String toString() {  return itemName= +this.itemName+ +Item.total=+this.item.getTotal();    }    //getter and setter...}

3) Spring EL--XML

The SpEL format is # {SpEL expression}. For xml configuration, see.
File: Spring-EL.xml


      
           
            
         
     
    
       
           
            
         
     
    
   
  

Note:

# {ItemBean} -- inject itemBean into the item attribute of customerBean. # {ItemBean. name} -- inject the name attribute of itemBean into the itemName attribute of customerBean.

4) Spring EL -- Annotation
The Annotation Version of SpEL.
Note: To use SpEL in Annotation, you must register the component through annotation. If you have registered bean in xml and defined @ Value in java class, @ Value will fail during runtime.
The Annotation version is equivalent to configuring it directly using Java code. XML hated users will be very interested in such features.
Item. java is as follows:

Package com. spring. hello; import org. springframework. beans. factory. annotation. value; import org. springframework. stereotype. component; @ Component (itemBean) public class Item {@ Value (itemA) // inject String private String name directly; @ Value (10) // inject integer private int total directly; // getter and setter ...}

Customer. java is as follows:

package com.spring.hello;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;  //getter and setter...}

Configure component auto-scan in Xml


      
   
  

In Annotation mode, use @ Value to define EL. In this case, a String and integer value are directly injected into itemBean, and itemBean is then injected into customerBean.
5) output results

App. java is as follows:

package com.spring.hello;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext(Spring-EL.xml);        Customer obj = (Customer) context.getBean(customerBean);        System.out.println(obj);    }}

The output result is as follows: itemName = itemA item. total = 10

Ii. Spring EL Method Invocation-SpEL Method call

SpEL allows developers to use El to run method functions and inject method return values into attributes.
1) Annotation Version of Spring EL Method Invocation

This section describes how to use @ Value annotation to call the SpEL method.
Customer. java is as follows:

Package com. spring. hello; import org. springframework. beans. factory. annotation. value; import org. springframework. stereotype. component; @ Component (customerBean) public class Customer {@ Value (# {'jinkai '. toUpperCase ()}) private String name; @ Value (# {priceBean. getSpecialPrice ()}) private double amount; // getter and setter... omit @ Override public String toString () {return Customer [name = + name +, amount = + amount +] ;}}

Price. java is as follows:

package com.spring.hello;import org.springframework.stereotype.Component;@Component(priceBean)public class Price {    public double getSpecialPrice() {        return new Double(99.99);    }}

Output result: Customer [name = jinkai, amount = 99.99]

In the preceding example, the following statement calls the toUpperCase () method.

@ Value ("# {'jinkai'. toUpperCase ()}")
Private String name;

In the preceding example, the following statement calls the getSpecialPrice () method in priceBean.

@ Value ("# {priceBean. getSpecialPrice ()}")
Private double amount;

2) XML version of Spring EL Method Invocation
Configure the following in XMl with the same effect


      
           
            
         
     
    
       
   
  

3. Spring EL Operators-SpEL Operator

Spring EL supports most mathematical, logical, and Relational operators.

Relational operators
Include: equal to (=, eq), not equal (! =, Ne), less than (<, lt), less than or equal to (<=, le), greater than (>, gt), greater than or equal to (> =, ge) (Note: the Xml configuration is as follows. Note: Replace "<;" with "<", and "<=" with "le".) logical operators
Including: and, or, and not (!) Mathematical Operators
Including addition (+), subtraction (-), multiplication (*), Division (/), modulo (%), and power index Code as follows:

Condition? True: false
For example:

 @Value(#{itemBean.qtyOnHand < 100 ? true : false})

Xml version

 
  

Spring EL supports values from List and Map sets.

For example:

//get map where key = 'MapA'    @Value(#{testBean.map['MapA']})    private String mapA;    //get first value from list, list is 0-based.    @Value(#{testBean.list[0]})    private String list;

Xml version

  
    
   
  

 

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.