SPRING EL

spring表達式java

 1 package spring;  2 
 3 import org.springframework.context.ApplicationContext;  4 import org.springframework.context.support.ClassPathXmlApplicationContext;  5 
 6 public class Circle {  7 
 8     private double radius;  9     private double peri; 10     private double area; 11 
12     public double getRadius() { 13         return radius; 14  } 15 
16     public void setRadius(double radius) { 17         this.radius = radius; 18  } 19 
20     public double getPeri() { 21         return peri; 22  } 23 
24     public void setPeri(double peri) { 25         this.peri = peri; 26  } 27 
28     public double getArea() { 29         return area; 30  } 31 
32     public void setArea(double area) { 33         this.area = area; 34  } 35 
36     public Circle(double radius) { 37         this.radius = radius; 38  } 39 
40  @Override 41     public String toString() { 42         return "Circle{" +
43                 "radius=" + radius +
44                 ", peri=" + peri +
45                 ", area=" + area +
46                 '}'; 47  } 48 
49     public static void main(String[] args){ 50         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spel.xml"); 51         Circle circle = ac.getBean("circle",Circle.class); 52  System.out.println(circle); 53         Email email = ac.getBean("email",Email.class); 54  System.out.println(email); 55  } 56 }
 1 package spring;  2 
 3 public class Email {  4 
 5     private String email;  6     private Circle circle;  7 
 8     public Email(String email) {  9         this.email = email; 10  } 11 
12     public Circle getCircle() { 13         return circle; 14  } 15 
16     public void setCircle(Circle circle) { 17         this.circle = circle; 18  } 19 
20     public String getEmail() { 21         return email; 22  } 23 
24     public void setEmail(String email) { 25         this.email = email; 26  } 27 
28  @Override 29     public String toString() { 30         return "Email{" +
31                 "email='" + email + '\'' +
32                 ", circle=" + circle +
33                 '}'; 34  } 35 
36 }

spring配置文件spel.xml正則表達式

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:p="http://www.springframework.org/schema/p"
 5  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7 
 8     <!--
 9  SPRING EL Spring表達式語言 10  * 是一個支持運行時查詢和操做對象圖的強大表達式語言 11  * 使用#{...}做爲定界符,在大括號內的就是SPEL 12  * SPEL爲bean的動態賦值提供便利 13 
14  SP EL能夠實現: 15  - 經過bean的id對bean進行引用 16  - 調用方法以及引用其餘對象中的屬性 17  - 計算表達式的值 18  - 支持算術運算符 + - * / % ^ 19  - 支持+號鏈接字符串 20  - 支持比較運算符 < > == <= >= lt gt eq le ge 21  - 支持邏輯運算符 and or not | 22  - 支持三目運算符 ?: 23  - 正則表達式的匹配 24  - 調用靜態方法或者靜態屬性 25 
26      -->
27 
28     <bean id="circle" class="spring.Circle">
29         <constructor-arg name="radius" value="50"/>
30         <!--調用靜態屬性Math.PI-->
31         <!--引用其餘對象中的屬性circle.radius-->
32         <!--算術運算 * ^-->
33         <property name="peri" value="#{T(java.lang.Math).PI * circle.radius *2}"/>
34         <property name="area" value="#{T(java.lang.Math).PI * circle.radius^2}"/>
35     </bean>
36 
37 
38     <bean id="email" class="spring.Email">
39         <constructor-arg name="email" value="SPELTest@126.com"/>
40         <!--引用其餘的bean-->
41         <property name="circle" value="#{circle}"/>
42         <!--正則表達式的匹配郵箱-->
43         <!--email最終的輸出結果 email='true'-->
44         <property name="email" value="#{email.email matches '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$'}"/>
45     </bean>
46 
47 
48 
49 
50 </beans>
相關文章
相關標籤/搜索