[原创]java WEB学习笔记100:Spring学习---Spring Bean配置:SpEL详细介绍及代码演示

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.Spring表达式语言:SpEL

  1)Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言

    2)语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL

  3)SpEL 为 bean 的属性进行动态赋值提供了便利

    4)通过 SpEL 可以实现:  

    ① 通过 bean 的 id 对 bean 进行引用  

    ② 调用方法以及引用对象中的属性

    ③ 计算表达式的值

    ④ 正则表达式的匹配

2.支持的运算

         

代码测试:

  

Car 
 1 package com.jason.spring.spel;
 2 
 3 public class Car {
 4     private String brand;
 5     private double price;
 6     
 7     //轮胎的周长
 8     private double tyrePerimeter;
 9     
10     public String getBrand() {
11         return brand;
12     }
13     public void setBrand(String brand) {
14         this.brand = brand;
15     }
16     public double getPrice() {
17         return price;
18     }
19     public void setPrice(double price) {
20         this.price = price;
21     }
22     
23     public double getTyrePerimeter() {
24         return tyrePerimeter;
25     }
26     public void setTyrePerimeter(double tyrePerimeter) {
27         this.tyrePerimeter = tyrePerimeter;
28     }
29     @Override
30     public String toString() {
31         return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter="
32                 + tyrePerimeter + "]";
33     }
34     
35     
36     
37     
38 
39 }
Person 
 1 package com.jason.spring.spel;
 2 
 3 public class Person {
 4     private String name;
 5     private Car car;
 6 
 7     // 引用address bean 的city属性
 8     private String city;
 9 
10     // 根据car 的price 确定info: car.price >= 300000 ? 金领 :白领
11     private String info;
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public Car getCar() {
22         return car;
23     }
24 
25     public void setCar(Car car) {
26         this.car = car;
27     }
28 
29     public String getCity() {
30         return city;
31     }
32 
33     public void setCity(String city) {
34         this.city = city;
35     }
36 
37     public String getInfo() {
38         return info;
39     }
40 
41     public void setInfo(String info) {
42         this.info = info;
43     }
44 
45     @Override
46     public String toString() {
47         return "Person [name=" + name + ", car=" + car + ", city=" + city
48                 + ", info=" + info + "]";
49     }
50 
51 }
beans-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     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6 
 7     <bean id="address" class="com.jason.spring.spel.Address">
 8         <!-- 使用spel 为属性赋一个字面值 -->
 9         <property name="city" value="#{'BeiJing'}"></property>
10         <property name="street" value="#{'Wudaokou'}"></property>
11     </bean>
12     
13     <bean id="car" class="com.jason.spring.spel.Car">
14         <property name="brand" value="Audi"></property>
15         <property name="price" value="500000"></property>
16         <!-- 使用 Spel 引用类的静态属性 -->
17         <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
18     </bean>
19     
20     
21     
22     <bean id="person" class="com.jason.spring.spel.Person">
23         <!-- 使用spel 来引用其他的bean -->
24         <property name="car" value="#{car}"></property>
25         <!-- 使用spel 来引用其他bean的 属性 -->
26         <property name="city" value="#{address.city}"></property>
27         <!-- 在spel 中使用运算符 -->
28         <property name="info" value="#{car.price > 300000 ? '金领' :'白领'}"></property>
29         <property name="name" value="Tom"></property>
30     </bean>
31 
32 </beans>
Main 
 1 package com.jason.spring.spel;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7     
 8     public static void main(String[] args) {
 9         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-spel.xml");
10         Address address = (Address) applicationContext.getBean("address");
11         System.out.println(address);
12         
13         Car car = (Car) applicationContext.getBean("car");
14         System.out.println(car);
15         
16         Person person = (Person) applicationContext.getBean("person");
17         System.out.println(person);
18     }
19 
20 }
原文地址:https://www.cnblogs.com/jasonHome/p/5967477.html