Spring3.0 入门进阶(2):SPEL用法大全

2024-02-14 11:48

本文主要是介绍Spring3.0 入门进阶(2):SPEL用法大全,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.xsdhttp://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省略
}







这篇关于Spring3.0 入门进阶(2):SPEL用法大全的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/708419

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

mysql中的group by高级用法

《mysql中的groupby高级用法》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,下面给大家介绍mysql中的groupby用法... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

Java中Scanner的用法示例小结

《Java中Scanner的用法示例小结》有时候我们在编写代码的时候可能会使用输入和输出,那Java也有自己的输入和输出,今天我们来探究一下,对JavaScanner用法相关知识感兴趣的朋友一起看看吧... 目录前言一 输出二 输入Scanner的使用多组输入三 综合练习:猜数字游戏猜数字前言有时候我们在

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用