Spring Expression Language(简称 SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图。语言语法类似于 Unified EL,但提供了额外的特性,最显著的是方法调用和基本的字符串模板功能。
文字表达式
布尔和关系运算符
正则表达式
类表达式
访问 properties, arrays, lists, maps
方法调用
关系运算符
参数
调用构造函数
Bean 引用
构造 Array
内嵌 lists
内嵌 maps
三元运算符
变量
用户定义的函数
集合投影
集合筛选
模板表达式
实验目标:实现属性注入文件,lis和进行逻辑运算
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>SpEL</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.7</version></dependency></dependencies></project>
读取属性文件,然后表达式注入list
@Value("#{'${server.name}'.split(',')}")private List<String> servers;
下面是一些常见的例子,更多的可以去官网看详细的文档,这里只给一个基本的入门
package com.et.spel.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.Resource;import org.springframework.stereotype.Component;public class BaseValueInject {private String normal; // 注入普通字符串private String systemPropertiesName; // 注入操作系统属性private double randomNumber; //注入表达式结果private String fromAnotherBean; // 注入其他Bean属性:注入beanInject对象的属性another,类具体定义见下面private Resource resourceFile; // 注入文件资源private Resource testUrl; // 注入URL资源public String toString() {return "BaseValueInject{" +"normal='" + normal + '\'' +", systemPropertiesName='" + systemPropertiesName + '\'' +", randomNumber=" + randomNumber +", fromAnotherBean='" + fromAnotherBean + '\'' +", resourceFile=" + resourceFile +", testUrl=" + testUrl +'}';}}
package com.et.spel.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;public class BeanInject {private String another;public String getAnother() {return another;}public void setAnother(String another) {this.another = another;}}
server:port: 8088name: aaa,bbb,ccc
以上只是一些关键代码,所有代码请参见下面代码仓库
https://github.com/Harries/springboot-demo
package com.et.spel;import com.et.spel.controller.BaseValueInject;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.expression.Expression;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(classes = DemoApplication.class)public class DemoTests {private Logger log = LoggerFactory.getLogger(getClass());@Beforepublic void before() {log.info("init some data");}@Afterpublic void after() {log.info("clean some data");}@Testpublic void execute() {log.info("your method test Code");}@Testpublic void stringSpel() {ExpressionParser parser = new SpelExpressionParser();Expression exp = parser.parseExpression("'Hello World'");String message = (String) exp.getValue();log.info(message);}@Testpublic void stringSpelConcat() {ExpressionParser parser = new SpelExpressionParser();Expression exp = parser.parseExpression("'Hello World'.concat('!')");String message = (String) exp.getValue();log.info(message);}@Autowiredprivate BaseValueInject baseValueInject;@Testpublic void baseValueInject() {System.out.println(baseValueInject.toString());}}
运行测试类,返回
2024-05-22 21:00:51.081 INFO 1868 --- [ main] com.et.spel.DemoTests : init some dataBaseValueInject{normal='normal', systemPropertiesName='Mac OS X', randomNumber=7.041399882847799, fromAnotherBean='another sss', resourceFile=class path resource [config.txt], testUrl=URL [http://www.baidu.com]}2024-05-22 21:00:51.085 INFO 1868 --- [ main] com.et.spel.DemoTests : clean some data
https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html
http://www.liuhaihua.cn/archives/710613.html
原文作者:HBLOG;如有侵权,请联系管理员删除。