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中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi