Spring学习-- SpEL表达式
本文内容纲要:
**Spring表达式语言(简称SpEL):**是一个支持运行时查询和操作对象图的强大的表达式语言。
**语法类似于EL:**SpEL使用#{...}作为定界符,所有在大括号中的字符都将被认为是SpEL,SpEL为bean的属性进行动态赋值提供了便利。
通过SpEL可以实现:
-
通过bean的id对bean进行引用。
-
调用方式以及引用对象中的属性。
-
计算表达式的值
-
正则表达式的匹配。
1 2<beansxmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 1packagecom.itdjx.spring.spel; 2 3/** 4*@authorWángChéngDá 5*@create2017-03-0213:26 6*/ 7publicclassCar{ 8 9privateStringbrand; 10 11privatedoubleprice; 12 13privateStringperimeter; 14 15publicStringgetBrand(){ 16returnbrand; 17} 18 19publicvoidsetBrand(Stringbrand){ 20this.brand=brand; 21} 22 23publicdoublegetPrice(){ 24returnprice; 25} 26 27publicvoidsetPrice(doubleprice){ 28this.price=price; 29} 30 31publicStringgetPerimeter(){ 32returnperimeter; 33} 34 35publicvoidsetPerimeter(Stringperimeter){ 36this.perimeter=perimeter; 37} 38 39@Override 40publicStringtoString(){ 41return"Car{"+ 42"brand='"+brand+'''+ 43",price="+price+ 44",perimeter='"+perimeter+'''+ 45'}'; 46} 47}
1packagecom.itdjx.spring.spel; 2 3/** 4*@authorWángChéngDá 5*@create2017-03-0213:48 6*/ 7publicclassAddress{ 8 9privateStringprovince; 10 11privateStringcity; 12 13privateStringarea; 14 15publicStringgetProvince(){ 16returnprovince; 17} 18 19publicvoidsetProvince(Stringprovince){ 20this.province=province; 21} 22 23publicStringgetCity(){ 24returncity; 25} 26 27publicvoidsetCity(Stringcity){ 28this.city=city; 29} 30 31publicStringgetArea(){ 32returnarea; 33} 34 35publicvoidsetArea(Stringarea){ 36this.area=area; 37} 38 39@Override 40publicStringtoString(){ 41return"Address{"+ 42"province='"+province+'''+ 43",city='"+city+'''+ 44",area='"+area+'''+ 45'}'; 46} 47}
1packagecom.itdjx.spring.spel; 2 3/** 4*@authorWángChéngDá 5*@create2017-03-0213:27 6*/ 7publicclassPerson{ 8 9privateStringname; 10 11privateintage; 12 13privatebooleanmarriage; 14 15privateCarcar; 16 17privateStringsocialStatus; 18 19privateStringaddress; 20 21publicStringgetName(){ 22returnname; 23} 24 25publicvoidsetName(Stringname){ 26this.name=name; 27} 28 29publicintgetAge(){ 30returnage; 31} 32 33publicvoidsetAge(intage){ 34this.age=age; 35} 36 37publicbooleanisMarriage(){ 38returnmarriage; 39} 40 41publicvoidsetMarriage(booleanmarriage){ 42this.marriage=marriage; 43} 44 45publicCargetCar(){ 46returncar; 47} 48 49publicvoidsetCar(Carcar){ 50this.car=car; 51} 52 53publicStringgetSocialStatus(){ 54returnsocialStatus; 55} 56 57publicvoidsetSocialStatus(StringsocialStatus){ 58this.socialStatus=socialStatus; 59} 60 61publicStringgetAddress(){ 62returnaddress; 63} 64 65publicvoidsetAddress(Stringaddress){ 66this.address=address; 67} 68 69@Override 70publicStringtoString(){ 71return"Person{"+ 72"name='"+name+'''+ 73",age="+age+ 74",marriage="+marriage+ 75",car="+car+ 76",socialStatus='"+socialStatus+'''+ 77",address='"+address+'''+ 78'}'; 79} 80}
packagecom.itdjx.spring.spel;
importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
@authorWángChéngDá
-
@create2017-03-0213:42 */ publicclassMain{
publicstaticvoidmain(String[]args){
ApplicationContextapp=newClassPathXmlApplicationContext("spel-beans.xml"); Personperson=(Person)app.getBean("person"); System.out.println(person);
} }
-
控制台输出:
SpEL字面量:
- 整数:#{8}
- 小数:#{8.8}
- 科学计数法:#{1e4}
- String:可以使用单引号或者双引号作为字符串的定界符号。
- Boolean:#{true}
SpEL引用bean,属性和方法:
- 引用其他对象:#{car}
- 引用其他对象的属性:#{car.brand}
- 调用其它方法,还可以链式操作:#{car.toString()}
- 调用静态方法静态属性:#{T(java.lang.Math).PI}
SpEL支持的运算符号:
- 算术运算符:+,-,*,/,%,^(加号还可以用作字符串连接)
- 比较运算符:<,>,==,>=,<=,lt,gt,eg,le,ge
- 逻辑运算符:and,or,not,|
- if-else运算符(类似三目运算符):?:(temary),?:(Elvis)
- 正则表达式:#{admin.emailmatches'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}
本文内容总结:
原文链接:https://www.cnblogs.com/chinda/p/6490896.html