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

相关文章

pytorch之torch.flatten()和torch.nn.Flatten()的用法

《pytorch之torch.flatten()和torch.nn.Flatten()的用法》:本文主要介绍pytorch之torch.flatten()和torch.nn.Flatten()的用... 目录torch.flatten()和torch.nn.Flatten()的用法下面举例说明总结torch

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

一文带你了解SpringBoot中启动参数的各种用法

《一文带你了解SpringBoot中启动参数的各种用法》在使用SpringBoot开发应用时,我们通常需要根据不同的环境或特定需求调整启动参数,那么,SpringBoot提供了哪些方式来配置这些启动参... 目录一、启动参数的常见传递方式二、通过命令行参数传递启动参数三、使用 application.pro