前言:java
当讨论依赖注入的时候,咱们一般所讨论的是将一个bean引用注入到另外一个bean的属性或构造器参数中。bean装配的另一个方面指的是将一个值注入到bean的属性或者构造器参数中。在没有学习使用怎么注入外部值时,咱们正常是直接将值写死在代码中。如将专辑的名字装配到BlankDisc bean的构造器或title属性中。正则表达式
例如,咱们可能按照这样的方式来组装BlankDisc:spring
若是使用XML的话,那么值也会是硬编码的:express
若是咱们可能会但愿避免硬编码值,而是想让这些值在运行时再肯定。为了实现这些功能,Spring提供了两种在运行时求值的方式:数组
在Spring中,处理外部值的最简单方式就是声明属性源并经过Spring的Environment来检索属性(使用@PropertySource注解和Environment)。例如,程序清单3.7展示了一个基本的Spring配置类,它使用外部的属性来装配BlankDisc bean。app
在本例中,@PropertySource引用了类路径中一个名为app.properties的文件。它大体会以下所示:dom
这个属性文件会加载到Spring的Environment中,稍后能够从这里检索属性。用getProperty()实现的。性能
1.一、Environment的getProperty()方法有四个重载的变种形式:学习
//获取属性值 若是找不到返回null String getProperty(String key); //获取属性值,若是找不到返回默认值 String getProperty(String key, String defaultValue); //获取指定类型的属性值,找不到返回null <T> T getProperty(String key, Class<T> targetType); //获取指定类型的属性值,找不到返回默认值 <T> T getProperty(String key, Class<T> targetType, T defaultValue);
1.二、Environment还提供了几个与属性相关的方法ui
//获取属性值,找不到抛出异常IllegalStateException String getRequiredProperty(String key) throws IllegalStateException; //检查一下某个属性是否存在 boolean containsProperty(String key); //获取属性值为某个Class类型,找不到返回null,若是类型不兼容将抛ConversionException <T> Class<T> getPropertyAsClass(String key, Class<T> targetType);
除了属性相关的功能之外,Environment还提供了一些方法来检查哪些profile处于激活状态:
Spring一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到Spring bean中。
占位符的形式为使用"${}"包装的属性名称,为了使用属性占位符,咱们必须配置一个PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer实例,从Spring 3.0开始,推荐使用PropertySourcesPlaceholderConfigurer,由于它可以基于Spring Environment及其属性源来解析占位符。
一、在基于Java配置中使用属性占位符注入属性
package chapter3.prctice6; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.stereotype.Component; @Component public class AppleMobile implements Mobile { private String color; private String type; public AppleMobile(@Value("${mobile.color}") String color, @Value("${mobile.type}") String type) { this.color = color; this.type = type; } @Bean public PropertySourcesPlaceholderConfigurer placeholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } public void play() { System.out.println(color+"-"+type); } }
二、在基于XML配置中使用占位符注入属性
<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="appleMobile" class="chapter3.prctice6.AppleMobile" c:color="${moble.color}" c:type="${mobile.type}"> </bean> <context:property-placeholder/> </beans>
解析外部属性可以将值的处理推迟到运行时,它的关注点在于根据名称解析来自于Spring Environment和属性源的属性
Spring 3引入了Spring表达式语言(Spring Expression Language,SpEL),它可以以一种强大和简洁的方式将值装配到bean属性和构造器参数中,在这个过程当中所使用的表达式会在运行时计算获得值。SpEL是相似于OGNL和JSF EL的表达式语言,可以在运行时构建复杂表达式,存取对象属性、对象方法调用等。全部的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL expression }
SpEL拥有不少特性,包括:
须要了解的第一件事情就是SpEL表达式要放到“#{ ... }”之中,这与属性占位符有些相似,属性占位符须要放到“${ ... }”之中。
//字面值表达式 #{1} //T()表达式会将java.lang.System视为Java中对应的类型,所以能够调用其static修饰的currentTimeMillis()方法。 #{T(System).currentTimeMillis()} //引用其余的bean或其余bean的属性(获得ID为sgtPeppers的bean的artist属性) #{sgtPeppers.artist} //经过systemProperties对象引用系统属性(proerty文件) #{systemProperties['disc.title']}
这只是SpEL的几个基础样例。在本章结束以前,你还会看到不少这样的表达式。可是,在此以前,让咱们看一下在bean装配的时候如何使用这些表达式。
当经过组件扫描建立bean的话,在注入属性和构造器参数时,咱们可使用@Value注解(必需要经过annotation注册组件才能够用)。这与以前看到的属性占位符很是相似。不过,在这里咱们所使用的不是占位符表达式,而是SpEL表达式。例如,下面的样例展示了BlankDisc,它会从系统属性中获取专辑名称和艺术家的名字:
在XML配置中,你能够将SpEL表达式传入<property>或<constructor-arg>的value属性中,或者将其做为p-命名空间或c-命名空间条目的值。
使用SpEL来表示整数字面量、浮点数、String值以及Boolean值。
//表示数值1 #{1} //表示浮点值 #{3.14159} //表示科学记数法,下面值:98,700 #{9.87E4} //表示String类型的字面值 #{'Hello'} //表示Boolean类型的值 #{false}
在SpEL中使用字面值其实没有太大的意思,只包含字面值状况并无太大的用处。SpEL表达式是由更简单的表达式组成d的。了解在SpEL中如何使用字面量仍是颇有用处的,当组合更为复杂的表达式时,你早晚会用到它们。
SpEL所能作的另一件基础的事情就是经过ID引用其余的bean。例如,你可使用SpEL将一个bean装配到另一个bean的属性中,此时要使用bean ID做为SpEL表达式(在本例中,也就是sgtPeppers):
//引用ID为sgtPeppers的Bean #{sgtPeppers} //表达式中引用sgtPeppers的artist属性 #{sgtPeppers.artist} //表达式中调用bean上的方法:调用bean的selectArtist()方法 #{artistSelector.selectArtist()} //对于被调用方法的返回值来讲,咱们一样能够调用它的方法。例如,若是selectArtist() //方法返回的是一个String,那么能够调用toUpperCase()将整个艺术家的名字改成大写 //字母形式: #{artistSelector.selectArtist().toUpperCase()} //使用了“?.”运算符。这个运算符可以在访问它右边的内容以前,确保它所对应的元素不是 //null。因此,若是selectArtist()的返回值是null的话,那么SpEL将不会调用toUpperCase() //方法。表达式的返回值会是null。(避免出现NullPointerException) #{artistSelector.selectArtist()?.toUpperCase()}
若是要在SpEL中访问类做用域的方法和常量的话,要依赖T()这个关键的运算符。例如,为了在SpEL中表达Java的Math类,须要按照以下的方式使用T()运算符:
//这里所示的T()运算符的结果会是一个Class对象表明了java.lang.Math。 #{T(java.lang.Math)} //T()运算符的真正价值在于它可以访问目标类型的静态方法和常量。 //获取类的静态属性 #{T(java.lang.Math).PI} //获取类的静态方法:计算获得一个0到1之间的随机数 #{T(java.lang.Math).random()}
这里所示的T()运算符的结果会是一个Class对象,T()运算符的真正价值在于它可以访问目标类型的静态方法和常量。与之相似,咱们能够调用T()运算符所获得类型的静态方法。咱们已经看到了经过T()调用System.currentTimeMillis()。
SpEL提供了多个运算符,这些运算符能够用在SpEL表达式的值上。以下表,概述了这些运算符。
运算符类型 | 运 算 符 |
算术运算 | + 、 - 、 * 、 / 、 % 、^ |
比较运算 | < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge |
逻辑运算 | and 、 or 、 not 、 │ |
条件运算 | ?: (ternary) 、 ?: (Elvis) |
正则表达式 | matches |
//这里是一个类的部分属性代码 @Value("#{1 == 1}") //true private boolean testEqual; @Value("#{1 != 1}") //false private boolean testNotEqual; @Value("#{1 < 1}") //false private boolean testLessThan; @Value("#{1 <= 1}") //true private boolean testLessThanOrEqual; @Value("#{1 > 1}") //false private boolean testGreaterThan; @Value("#{1 >= 1}") //true private boolean testGreaterThanOrEqual; //Logical operators , numberBean.no == 999 @Value("#{numberBean.no == 999 and numberBean.no < 900}") //false private boolean testAnd; @Value("#{numberBean.no == 999 or numberBean.no < 900}") //true private boolean testOr; @Value("#{!(numberBean.no == 999)}") //false private boolean testNot; //Mathematical operators @Value("#{1 + 1}") //2.0 private double testAdd; @Value("#{'1' + '@' + '1'}") //1@1 private String testAddString; @Value("#{1 - 1}") //0.0 private double testSubtraction; @Value("#{1 * 1}") //1.0 private double testMultiplication; @Value("#{10 / 2}") //5.0 private double testDivision; @Value("#{10 % 10}") //0.0 private double testModulus ; @Value("#{2 ^ 2}") //4.0 private double testExponentialPower; //-------------------------结果:--------------- testEqual=true, testNotEqual=false, testLessThan=false, testLessThanOrEqual=true, testGreaterThan=false, testGreaterThanOrEqual=true, testAnd=false, testOr=true, testNot=false, testAdd=2.0, testAddString=1@1, testSubtraction=0.0, testMultiplication=1.0, testDivision=5.0, testModulus=0.0, testExponentialPower=4.0
当处理文本时,有时检查文本是否匹配某种模式是很是有用的。SpEL经过matches运算符支持表达式中的模式匹配。matches运算符对String类型的文本(做为左边参数)应用正则表达式(做为右边参数)。matches的运算结果会返回一个Boolean类型的值:若是与正则表达式相匹配,则返回true;不然返回false。
一、SpEL中最使人惊奇的一些技巧是与集合和数组相关的。最简单的事情可能就是引用列表中的一个元素了:
二、为了让这个表达式更丰富一些,假设咱们要从jukebox中随机选择一首歌:
三、它还能够从String中获取一个字符。下标基于零开始,也就结果为"s",以下:
四、SpEL还提供了查询运算符( .?[ ] ),它会用来对集合进行过滤,获得集合的一个子集。假设你但愿获得jukebox中artist属性为Aerosmith的全部歌曲。
以看到,选择运算符在它的方括号中接受另外一个表达式。当SpEL迭代歌曲列表的时候,会对歌曲集合中的每个条目计算这个表达式。若是表达式的计算结果为true的话,那么条目会放到新的集合中。不然的话,它就不会放到新集合中。
五、SpEL还提供了另外两个查询运算符:“.^[ ]”和“.$[ ]”,它们分别用来在集合中查询第一个匹配项和最后一个匹配项。
六、SpEL还提供了投影运算符(.![ ]),它会从集合的每一个成员中选择特定的属性放到另一个集合中。做为样例,假设咱们不想要歌曲对象的集合,而是全部歌曲名称的集合。以下的表达式会将title属性投影到一个新的String类型的集合中:
实际上,投影操做能够与其余任意的SpEL运算符一块儿使用。好比,咱们可使用以下的表达式得到Aerosmith全部歌曲的名称列表: