Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅.
作为入门,本篇主要介绍SPEL的使用.
SPEL(Spring Expression Language)是Spring 3引入的一个新特性,通过使用SPEL可以在程序运行的过程中,动态的对BEAN的属性进行赋值,话不多说,请看代码以及注释
入口类
package com.eric.introduce.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 主要用来演示SPEL的用法
*
* @author Eric
*
*/
public class SpelCaller {
private static final String CONFIG = "com/eric/introduce/spel/spel.xml";
private static ApplicationContext context = new ClassPathXmlApplicationContext(
CONFIG);
public static void main(String[] args) {
//下面的几个方法主要是和操作Bean相关
getNormalValue();
getOtherBeanProperties();
callOtherBeanMethod();
getOtherBeanPropertiesByCheckSafe();
getPropertiesByStaticField();
//下面的几个方法主要是和操作集合相关
getPropertyFromList();
getAddressFromListByRandom();
getAddressFromMap();
getPropertiesFromProperties();
getCityListByCondiciton();
getFilterResult1st();
getFilterResultLast();
getFilterResultFieldList();
}
/**
* 打印几个常数值
*/
public static void getNormalValue() {
Student testClass = (Student) context.getBean("eric");
System.out.println(testClass.getName());
System.out.println(testClass.getAge());
System.out.println(testClass.isGender());
}
/**
* 利用SPEL从别的Bean属性中获得属性值
*/
public static void getOtherBeanProperties() {
Student testClass = (Student) context.getBean("eric");
System.out.println(testClass.getAddress());
}
/**
* 利用SPEL从别的Bean的方法调用中获得属性值
*/
public static void callOtherBeanMethod() {
Student testClass = (Student) context.getBean("eric");
System.out.println(testClass.getEmail());
}
/**
* get properties value by "Check Safe"
*/
public static void getOtherBeanPropertiesByCheckSafe() {
Student testClass = (Student) context.getBean("eric");
System.out.println(testClass.getDescription());
}
/**
* get properties value by static field/Method 此外这个例子中还演示了SPEL还支持
* 算数运算(+,-,*,/....) 关系运算(<,>,==,<=,>=...) 逻辑运算(and,or,not) 条件运算(?:)
* 正则表达式(matches)
* */
public static void getPropertiesByStaticField() {
Student testClass = (Student) context.getBean("eric");
System.out.println(testClass.getRandomId());
System.out.println(testClass.getPi());
}
/**
* 利用SPEL随机从列表中选择一个地址
*/
public static void getPropertyFromList() {
Student testClass = (Student) context.getBean("getAddressFromList");
System.out.println(testClass.getCity());
}
/**
* 利用SPEL随机从列表中随机一个地址
*/
public static void getAddressFromListByRandom() {
Student testClass = (Student) context
.getBean("getAddressFromListByRandom");
System.out.println(testClass.getCity());
}
/**
* 利用SPEL从Map中选择一个地址
*/
public static void getAddressFromMap() {
Student testClass = (Student) context.getBean("getAddressFromMap");
System.out.println(testClass.getCity());
}
/**
* 利用SPEL从properties选择地址
*/
public static void getPropertiesFromProperties() {
Student testClass = (Student) context
.getBean("getPropertiesFromProperties");
System.out.println(testClass.getUsername());
System.out.println(testClass.getPwd());
}
/**
* 利用SPEL从城市列表中选择地址人口大于123456的城市
*/
public static void getCityListByCondiciton() {
Student testClass = (Student) context
.getBean("getCityListByCondiciton");
System.out.println(testClass.getFavCities());
}
/**
* 利用SPEL从城市列表中选择地址人口大于123456的结果中的第一个
*/
public static void getFilterResult1st() {
Student testClass = (Student) context.getBean("getFilterResult1st");
System.out.println(testClass.getBestCity());
}
/**
* 利用SPEL从城市列表中选择地址人口大于123456的结果中的最後一个
*/
public static void getFilterResultLast() {
Student testClass = (Student) context.getBean("getFilterResultLast");
System.out.println(testClass.getWorCity());
}
/**
* 利用SPEL从城市列表中选择地址人口大于123456的结果的所有名字
*/
public static void getFilterResultFieldList() {
Student testClass = (Student) context
.getBean("getFilterResultFieldList");
System.out.println(testClass.getFavCitieNames());
}
}
配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 用来模拟SPEL对集合的操作 -->
<util:list id="cities">
<bean class="com.eric.introduce.spel.City" p:name="NJ" p:state="JS"
p:population="123456" />
<bean class="com.eric.introduce.spel.City" p:name="HZ" p:state="ZJ"
p:population="1231232" />
<bean class="com.eric.introduce.spel.City" p:name="SJ" p:state="JS"
p:population="778865" />
</util:list>
<!-- 用来模拟SPEL对MAP的操作 -->
<util:map id="citiesmap">
<entry key="nanjing" value-ref="nanjing" />
<entry key="hangzhou" value-ref="hangzhou" />
</util:map>
<!-- 用来模拟SPEL对Properties的操作 -->
<util:properties id="authorization"
location="classpath:com/eric/introduce/spel/account.properties" />
<bean id="nanjing" class="com.eric.introduce.spel.City" p:name="NJ"
p:state="JS" p:population="123456" />
<bean id="hangzhou" class="com.eric.introduce.spel.City" p:name="HZ"
p:state="ZJ" p:population="1231232" />
<!-- 通过Index从list中选择地址 -->
<bean id="getAddressFromList" class="com.eric.introduce.spel.Student">
<property name="city" value="#{cities[2]}"></property>
</bean>
<!-- 随机从list中选择地址 -->
<bean id="getAddressFromListByRandom" class="com.eric.introduce.spel.Student">
<property name="city"
value="#{cities[T(java.lang.Math).random()*cities.size()]}"></property>
</bean>
<!-- 从map选择地址 -->
<bean id="getAddressFromMap" class="com.eric.introduce.spel.Student">
<property name="city" value="#{citiesmap['hangzhou']}"></property>
</bean>
<!-- 从properties选择地址 -->
<bean id="getPropertiesFromProperties" class="com.eric.introduce.spel.Student">
<property name="username" value="#{authorization['username']}" />
<property name="pwd" value="#{authorization['password']}" />
</bean>
<!-- 从城市列表中选择地址人口大于123456的城市 -->
<bean id="getCityListByCondiciton" class="com.eric.introduce.spel.Student">
<property name="favCities" value="#{cities.?[population gt 123456]}" />
</bean>
<!-- 从城市列表中选择地址人口大于123456的结果中的第一个 -->
<bean id="getFilterResult1st" class="com.eric.introduce.spel.Student">
<property name="bestCity" value="#{cities.^[population gt 123456]}" />
</bean>
<!-- 从城市列表中选择地址人口大于123456的结果中的最後一个 -->
<bean id="getFilterResultLast" class="com.eric.introduce.spel.Student">
<property name="worCity" value="#{cities.$[population gt 123456]}" />
</bean>
<!-- 从城市列表中选择地址人口大于123456的结果的所有名字 -->
<bean id="getFilterResultFieldList" class="com.eric.introduce.spel.Student">
<property name="favCitieNames"
value="#{cities.?[population gt 123456].![name+'_'+state]}" />
</bean>
<bean id="eric" class="com.eric.introduce.spel.Student">
<!-- 表达常量值 -->
<property name="name" value="#{'Eric'}"></property>
<property name="age" value="#{28}"></property>
<property name="gender" value="#{true}"></property>
<!-- reference other bean properties -->
<property name="address" value="#{simon.address}"></property>
<!-- get properties value from other bean method -->
<property name="email" value="#{simon.getEmailByName().toUpperCase()}"></property>
<!-- get properties value by "Check Safe" -->
<property name="description" value="#{simon.getDescription()?.toUpperCase()}"></property>
<!-- get properties value by static field/Method -->
<property name="randomId" value="#{T(java.lang.Math).random()*1000}"></property>
<property name="pi" value="#{T(java.lang.Math).PI}"></property>
</bean>
<bean id="simon" class="com.eric.introduce.spel.Student">
<property name="name" value="#{'Simon'}"></property>
<property name="address" value="#{'WUHAN'}"></property>
</bean>
</beans>
其他主要相关类
package com.eric.introduce.spel;
import java.util.List;
public class Student {
private int randomId;
private float pi;
private String name;
private boolean gender;
private Integer age;
private String address;
private String email;
private String description;
private City city;
private String username;
private String pwd;
private List<City> favCities;
private List<String> favCitieNames;
private City bestCity;
private City worCity;
//......get/Set省略
}
package com.eric.introduce.spel;
public class City {
private String name;
private String state;
private int population;
//......get/Set省略
}
分享到:
相关推荐
◆富Portlet 2.0支持:Spring MVC完全支持Portlet 2.0环境以及Portlet 2.0中新的事件与资源请求模型。 ◆对象/XML映射(OXM):原本在Spring Web服务中提供,现在被纳入Spring框架核心。 ◆下一代计划能力:新的...
Spring 3.0中,新的表达式语言名叫Spring Expression Language(SpEL,直译就是Spring表达式语言)。Shaun将其描述为“嵌入在Spring编程模型上的,可以用来在Spring项目之间描述任务以及配置的微型脚本语言。SpEL...
Spring 3.0是Spring框架的一次重大更新,而根据SpringSource副总裁Shaun Connolly的描述,Spring框架是当下最流行的Java开发框架。 全新的Spring 3.0提供了全面的RESTful Web服务支持,以及一个新的表达式语言。其...
SIA实战(一):SpEL的使用 源码
NULL 博文链接:https://364232252.iteye.com/blog/2369770
'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观...
spring spEL 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring Cloud Function 是基于Spring Boot 的函数计算框架,它抽象出所有传输细节和基础架构,允许开发人员保留所有熟悉的工具和流程,并专注于业务逻辑。 批量检测脚本:python Spel_RCE_POC.py url.txt 反弹...
SSH笔记-Spring表达式语言:SpEL,关于SpEl的字面量、引用 Bean、属性和方法、支持的运算符号
Spring框架是Java开发中广泛使用的一个强大工具,它通过依赖注入和控制反转等核心概念,极大地提高了开发效率和应用的可维护性。在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@...
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看
模板化 使用spel进行简单的模板制作
* * * * Core Container(核心容器) Spring的核心容器是其他模块建立的基础,它主要由Beans模块、Core模块、Context模块、Context-support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,...
主要介绍了spring表达式语言SpEL用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Spring spel表达式使用方法示例,通过一些实例向大家展示了spel表达式的用法,需要的朋友可以了解下。
spring基础,主要讲解了 spring的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用
SpringCloud Function SpEL注入漏洞分析(CVE-2022-22963).doc
Spring Expression Language (SpEL)中文文档。基于Spring4.x。