spring表达式
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>