Spring学习笔记--在SpEL中筛选集合
要用到Spring的util(包括util:list等),xml文件中的beans中需要添加一些有关util的信息:
<?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" 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.xsd"> <!-- bean definitions here --> </beans>
首先定义一个City类。
package com.moonlit.myspring;
public class City {
private String name;
private String state;
private int population;
@Override
public String toString() {
return String.format("[%s,%s,%d]", name, state, population);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
使用<util:list>元素在Spring里配置一个包含City对象的List集合。
<util:list id="cities">
<bean class="com.moonlit.myspring.City"
p:name="Beijing" p:state="BJ" p:population="215213241" />
<bean class="com.moonlit.myspring.City"
p:name="Shanghai" p:state="SH" p:population="242612378" />
<bean class="com.moonlit.myspring.City"
p:name="Guangzhou" p:state="GD" p:population="12700812" />
<bean class="com.moonlit.myspring.City"
p:name="Hangzhou" p:state="HZ" p:population="8700420" />
<bean class="com.moonlit.myspring.City"
p:name="Baoding" p:state="BD" p:population="11194443" />
<bean class="com.moonlit.myspring.City"
p:name="Harbin" p:state="HRB" p:population="10636003" />
<bean class="com.moonlit.myspring.City"
p:name="Shuzhou" p:state="SZ" p:population="10466092" />
<bean class="com.moonlit.myspring.City"
p:name="Xian" p:state="XA" p:population="8467821" />
</util:list>
<util:list>元素是由Spring的util命名空间所定义的。它创建了一个java.util.List类型的Bean,这个集合里包含了所有以上配置的Bean。
<util:list>对象也是一个Bean,我们可以通过以下方式获得cities:
List<City> cities = (List<City>) context.getBean("cities");
可以通过如下方式访问集合成员:
<property name="chosenCity" value="#{cities[2]}" />
<property name="randomChosenCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}" />
中括号([])运算符会始终通过索引访问集合中的成员。
[]运算符同样可以用来获取java.util.Map集合中的成员。例如,假设City对象以其名字作为键放入Map集合中。在这种场景下,我们可以像下面展示的那样获取键为Dallas的entry:
<property name="choseCity" value="#{cities['Dallas']}" />
[]运算符还可以从java.util.Properties集合中获取值。例如,假设我们需要通过<util:properties>元素在Spring中加载一个properties配置文件:
<util:properties id="settings" location="classpath:setting.properties" />
这里定义的settings Bean是一个java.util.Propertes类,夹在了一个名为名为settings.properties的文件。我们可以使用SpEL获得settings Bean中名为twitter.accessTokend的对象:
<properties name="accessToken" value="#{settings['twitter.accessToken']}" />
除了访问<util:properties>所生命的集合中的属性,Spring还为SpEL选择了两种特殊的选择属性的方式:systemEnvironment和systemProperties。
systemEnvironment包含了应用程序所在机器上的所有环境变量。
<property name="homePath" value="${systemEnvironment['HOME']}" />
systemPropreties包含了Java应用程序启动时所设置的所有属性(通常通过-D参数)。例如,如果使用-Dapplication.home=/etc/myapp,来启动JVM,那么你就可以通过以下SpEL表达式将该值注入homePath属性中:
<property name="homePath" value="#{systemProperties['application.home']}" />
[]运算符同样可以通过索引来得到字符串的某个字符。例如,下面的表达式返回“s”:
'This is a test'[3]
查询集合成员 —— 查询运算符(.?[])
<property name="bigCities" value="#{cities.?[population gt 2e7]}" />
查询运算符会创建一个新的集合,新的集合中只存放符合中括号内的表达式的成员。这里,bigCIties属性被注入了人口多于20000000的城市集合。
SpEL还提供了两种其他查询运算符:
“.^[]”:从集合中查询第一个匹配项。
“.$[]”:从集合中查询最后一个匹配项。
例如:
<property name="aBigCity" value="#{cities.^[population gt 2e7]}" />
<property name="aBigCity" value="#{cities.$[population gt 2e7]}" />
投影集合 —— 投影运算符(.![])
集合投影是从集合的每一个元素中选择特定的属性放入一个新的集合中。
例如,从cities中获得所有的name:
<property name="cityNames" value="#{cities.![name]}" />
查询运算符.?[]和投影运算符.![]可以混合使用。
<property name="sityNames" value="#{cities.?[population gt 2e7].![name + ',' + state]}" />
演示上面部分效果的程序:
package com.moonlit.myspring; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Hero {
private City chosenCity;
private City randomChosenCity;
private String homePath;
private List<City> bigCities;
private List<String> cityNames;
public void perform() {
System.out.println("chosen city = " + chosenCity);
System.out.println("random chosen city = " + randomChosenCity);
System.out.println("home path = " + homePath);
System.out.println("big cities:");
for (City city : bigCities)
System.out.println("\t" + city);
System.out.println("city names :");
for (String cityName : cityNames)
System.out.println("\t" + cityName);
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-util-idol.xml");
// util:list is also a bean
List<City> cities = (List<City>) context.getBean("cities");
for (City city : cities)
System.out.println(city);
Hero hero = (Hero) context.getBean("hero");
hero.perform(); }
public City getChosenCity() {
return chosenCity;
}
public void setChosenCity(City chosenCity) {
this.chosenCity = chosenCity;
}
public City getRandomChosenCity() {
return randomChosenCity;
}
public void setRandomChosenCity(City randomChosenCity) {
this.randomChosenCity = randomChosenCity;
}
public String getHomePath() {
return homePath;
}
public void setHomePath(String homePath) {
this.homePath = homePath;
}
public List<City> getBigCities() {
return bigCities;
}
public void setBigCities(List<City> bigCities) {
this.bigCities = bigCities;
}
public List<String> getCityNames() {
return cityNames;
}
public void setCityNames(List<String> cityNames) {
this.cityNames = cityNames;
} }
Hero.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" 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.xsd"> <!-- bean definitions here --> <util:list id="cities">
<bean class="com.moonlit.myspring.City"
p:name="Beijing" p:state="BJ" p:population="21521324" />
<bean class="com.moonlit.myspring.City"
p:name="Shanghai" p:state="SH" p:population="24261237" />
<bean class="com.moonlit.myspring.City"
p:name="Guangzhou" p:state="GD" p:population="12700812" />
<bean class="com.moonlit.myspring.City"
p:name="Hangzhou" p:state="HZ" p:population="8700420" />
<bean class="com.moonlit.myspring.City"
p:name="Baoding" p:state="BD" p:population="11194443" />
<bean class="com.moonlit.myspring.City"
p:name="Harbin" p:state="HRB" p:population="10636003" />
<bean class="com.moonlit.myspring.City"
p:name="Shuzhou" p:state="SZ" p:population="10466092" />
<bean class="com.moonlit.myspring.City"
p:name="Xian" p:state="XA" p:population="8467821" />
</util:list> <bean id="hero" class="com.moonlit.myspring.Hero">
<property name="chosenCity" value="#{cities[2]}" />
<property name="randomChosenCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}" />
<property name="homePath" value="#{systemEnvironment['HOME']}" />
<property name="bigCities" value="#{cities.?[population gt 2e7]}" />
<property name="cityNames" value="#{cities.![name]}" />
</bean> </beans>
spring-util-idol.xml
扩展阅读(Spring4.0.x doc里面的xml配置信息和很多例子):http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/xsd-config.html
理解:
<util:list>,<util:map>,<util:properties>可以装配集合,使用前请声明util命名空间。
通过<util:properties id="XX" location="classpath:YY.properties">读取YY.properties文件。
查询运算符:
.?[]:查询所有匹配项
.^[]:查询第一个匹配项
.$[]:查询最后一个匹配项
投影运算符:
.![]
Spring学习笔记--在SpEL中筛选集合的更多相关文章
- Spring实战学习笔记之SpEL表达式
在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现 SpEL表达式的首要目标是通过计算获得某个值. ...
- Spring学习笔记(一)
Spring学习笔记(一) 这是一个沉淀的过程,大概第一次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了一个很 ...
- Spring学习笔记2——表单数据验证、文件上传
在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- Spring学习笔记之aop动态代理(3)
Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...
- Spring学习笔记之依赖的注解(2)
Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
随机推荐
- JS地毯式学习四
1 窗口的位置 用来确定和修改 window 对象位置的属性和方法有很多. IE . Safari . Opera 和 Chrome都提供了 screenLeft 和 screenTop 属性,分别 ...
- 3DS更新R4烧录卡内核
机子是N3DSLL,用的R4烧录卡是银卡HK版. 关于R4烧录卡的基础知识科普贴: https://tieba.baidu.com/p/4855297365 为了防止该网页挂掉还是存图吧. 找最新内核 ...
- dom4j解析带命名空间的xml文件
文件内容如下 <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=& ...
- kafkaStream执行过程中出现TimeoutException异常退出
日志中出现以下异常信息,程序中断退出. 目前参考别人的修改下面的配置,原来使用的hostname,改成IP,再观察观察. advertised.listeners=PLAINTEXT://192.1 ...
- git中文乱码解决方案
解决方案: 在bash提示符下输入: git config --global core.quotepath false core.quotepath设为false的话,就不会对0x80以上的字符进行q ...
- pip使用国内镜像,豆瓣、清华
pip使用国内镜像,豆瓣.清华 2017年01月18日 22:27:44 阅读数:4416 Python开发的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导 ...
- JQuery下拉控件select的操作汇总
JQuery获取和设置Select选项方法汇总如下: 获取select 先看看下面代码: $("#select_id").change(function(){//code... ...
- 谈谈Java中整数类型(short int long)的存储方式
在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对ja ...
- Struts2中jsp前台传值到action后台的三种方式以及valueStack的使用
struts2中的Action接收表单传递过来的参数有3种方法: 如,登陆表单login.jsp: <form action="login" method="pos ...
- 用X264编码以后的H264数据
输入的数据准备好了,编码后的数据都在x264_nal_t的数组.我这里设置的参数是Baseline Profile,所以编码后没有B帧,将编码后的数据保存分析后发现,第一次编码的时候会有4个NAl,分 ...