Spring expression Language (spel) brief and Hello World example

Source: Internet
Author: User

As one of spring's basic modules, the Spring Expression Language module (Spring Expressions Language, abbreviated SPEL) provides a powerful expression language for querying and manipulating an object graph at run time.

Spring El can be configured either through XML or by annotations. Here's a simple example of how to configure Spel in two different ways to inject string, Integer, and JavaBean data.


    1. Spring El Dependent

      The project is managed through MAVEN, and the core dependency of spring is declared in Maven's pom.xml configuration file, and Maven automatically downloads the dependent packages. (If you are not using MAVEN for project management, you can download and add the jar package to BuildPath directly)

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

Defines two simple beans, which are then injected with Spel for the Bean's attributes, respectively, in XML and annotated form.

public class Customer {private Item item;private String itemname;} public class Item {private String name;private int qty;}


3. Configuring the spring EL by way of XML

The SPEL expression is formatted as follows #{spel expression},xml The bean definition file in the configuration Spel, as shown below

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// 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=" ten "/></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 property of Customerbean

#{itembean.name}-injects the Itembean Name property into the ItemName property of Customerbean


4. Configure Spring EL with annotations

Note: To configure Spel with annotations, the bean must be registered by annotation. If you register the bean in the XML configuration file and define @value in the Java class, @Value will be invalidated.

The annotation mode 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 directlyprivate string name; @Value ("10")// Inject interger directlyprivate 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd " ><context:component-scan/></beans>

In annotation mode, the Springel is defined by @value. With the above configuration, the string value and the integer value are injected directly into the Itembean property, and the Itembean is injected into the Customerbean property.


5. Output

Running the following code, either XML or annotation, 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:

Customer [Item=item [Name=itema, qty=10], Itemname=itema]

Similar to JSF EL, Spel supports a number of other expressions, including relational expressions, regular expressions, arrays, lists, and so on, in addition to setting values directly.


Reference

http://www.mkyong.com/spring3/spring-el-hello-world-example/

http://examples.javacodegeeks.com/enterprise-java/spring/spel/spring-expression-language-example/


This article is from the "Wood on the Road" blog, please be sure to keep this source http://mumufairy.blog.51cto.com/6184508/1694958

Spring expression Language (spel) brief and Hello World example

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.