1.什么SPEL?
Spring Expression Language(简称 SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图。语言语法类似于 Unified EL,但提供了额外的特性,最显著的是方法调用和基本的字符串模板功能。
SpEL主要功能
2.代码工程
实验目标:实现属性注入文件,lis和进行逻辑运算
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
bean
读取属性文件,然后表达式注入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;
@Component
public class BaseValueInject {
@Value("normal")
private String normal; // 注入普通字符串
@Value("#{systemProperties['os.name']}")
private String systemPropertiesName; // 注入操作系统属性
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber; //注入表达式结果
@Value("#{beanInject.another}")
private String fromAnotherBean; // 注入其他Bean属性:注入beanInject对象的属性another,类具体定义见下面
@Value("classpath:config.txt")
private Resource resourceFile; // 注入文件资源
@Value("http://www.baidu.com")
private Resource testUrl; // 注入URL资源
@Override
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;
@Component
public class BeanInject {
@Value("another sss")
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
application.yaml
server:
port: 8088
name: aaa,bbb,ccc
3.测试
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());
@Before
public void before() {
log.info("init some data");
}
@After
public void after() {
log.info("clean some data");
}
@Test
public void execute() {
log.info("your method test Code");
}
@Test
public void stringSpel() {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();
log.info(message);
}
@Test
public void stringSpelConcat() {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.concat('!')");
String message = (String) exp.getValue();
log.info(message);
}
@Autowired
private BaseValueInject baseValueInject;
@Test
public void baseValueInject() {
System.out.println(baseValueInject.toString());
}
}
运行测试类,返回
2024-05-22 21:00:51.081 INFO 1868 --- [ main] com.et.spel.DemoTests : init some data
BaseValueInject{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
4.引用