Spring Introductory Lesson Tenth

Source: Internet
Author: User

Spring Expression Language: Spel

The Spring expression language (spel) is a powerful expression language that supports run-time querying and manipulating object graphs.

Syntax similar to El:spel using #{...} As delimiters, all the characters in the curly braces will be considered spel

Spel provides the convenience for dynamic replication of bean properties.

The Spel can be implemented by:

-The bean is referenced by the Bean's ID

-Invoking methods and referencing properties in an object

-evaluates the value of an expression

-Matching of regular expressions

See below how to use

 PackageLogan.spring.study.spel; Public classCar {PrivateString brand; Private intPrice ; Private DoubleTyreperimeter;  PublicString Getbrand () {returnbrand; }     Public voidSetbrand (String brand) { This. Brand =brand; }     Public intGetPrice () {returnPrice ; }     Public voidSetprice (intPrice ) {         This. Price =Price ; }     Public DoubleGettyreperimeter () {returnTyreperimeter; }     Public voidSettyreperimeter (Doubletyreperimeter) {         This. Tyreperimeter =Tyreperimeter; } @Override PublicString toString () {return"Car [brand=" + Brand + ", price=" + Price + ", tyreperimeter=" + Tyreperimeter + "]"; }}
 PackageLogan.spring.study.spel; Public classAddress {PrivateString City; PrivateString Street;  PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     PublicString Getstreet () {returnStreet; }     Public voidSetstreet (String Street) { This. Street =Street; } @Override PublicString toString () {return"Address [city=" + City + ", street=" + Street + "]"; }        }
 PackageLogan.spring.study.spel; Public classPerson {PrivateString name; Privatecar Car; //referencing the city property of the address bean    PrivateString City; //according to the price of car to determine info:car price >=300000 gold collar, otherwise white-collar    PrivateString Info;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicCar Getcar () {returncar; }     Public voidSetcar (car car) { This. Car =car; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     PublicString GetInfo () {returninfo; }     Public voidsetInfo (String info) { This. info =info; } @Override PublicString toString () {return"Person [name=" + name + ", car=" + Car + ", city=" + City + ", info=" + info + "]"; }    }

Configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd" >        <BeanID= "Address"class= "Logan.spring.study.spel.Address">        <!--use Spel to assign a literal to a property -        < Propertyname= "City"value= "#{' Beijing '}"></ Property>        < Propertyname= "Street"value= "Wudaokou"></ Property>    </Bean>        <BeanID= "Car"class= "Logan.spring.study.spel.Car">        < Propertyname= "Brand"value= "Audi"></ Property>        < Propertyname= "Price"value= "500000"></ Property>        <!--Using Spel to use static properties of a class -        < Propertyname= "Tyreperimeter"value= "#{t (java.lang.Math). PI * +} "></ Property>    </Bean>        <BeanID= "Person"class= "Logan.spring.study.spel.Person">        < Propertyname= "Car"value= "#{car}"></ Property>        < Propertyname= "City"value= "#{address.city}"></ Property>        < Propertyname= "Info"value= "#{car.price > 30?" ' Golden collar ': ' White Collar '} '></ Property>        < Propertyname= "Name"value= "Tom"></ Property>    </Bean></Beans>
 PackageLogan.spring.study.spel;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext CTX=NewClasspathxmlapplicationcontext ("Beans-spel.xml"); Address Address= (address) Ctx.getbean ("Address");                SYSTEM.OUT.PRINTLN (address); Car Car= (CAR) ctx.getbean ("Car"));                SYSTEM.OUT.PRINTLN (car); Person Person= (person) ctx.getbean (' person ');    SYSTEM.OUT.PRINTLN (person); }}

Here are the output results

May 10:01:5010:01:50 CST, 10:01:50Class path resource [beans-< c4>spel.xml]address [City =beijing, street=wudaokou]car [Brand=audi, price=500000, tyreperimeter= 251.32741228718345]person [name=tom, Car=car [Brand=audi, price=500000, tyreperimeter=251.32741228718345], city=beijing, info= Gold collar]

Spring Introductory Lesson Tenth

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.