zoukankan      html  css  js  c++  java
  • spring学习总结——高级装配学习四(运行时:值注入、spring表达式)

    前言:

      当讨论依赖注入的时候,我们通常所讨论的是将一个bean引用注入到另一个bean的属性或构造器参数中。bean装配的另外一个方面指的是将一个值注入到bean的属性或者构造器参数中。在没有学习使用怎么注入外部值时,我们正常是直接将值写死在代码中。如将专辑的名字装配到BlankDisc bean的构造器或title属性中。

    例如,我们可能按照这样的方式来组装BlankDisc:

    如果使用XML的话,那么值也会是硬编码的:

     如果我们可能会希望避免硬编码值,而是想让这些值在运行时再确定。为了实现这些功能,Spring提供了两种在运行时求值的方式:

    1. 属性占位符(Property placeholder)。
    2. Spring表达式语言(SpEL)

    一、注入外部的值

    在Spring中,处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性(使用@PropertySource注解和Environment)。例如,程序清单3.7展现了一个基本的Spring配置类,它使用外部的属性来装配BlankDisc bean。

    在本例中,@PropertySource引用了类路径中一个名为app.properties的文件。它大致会如下所示:

    这个属性文件会加载到Spring的Environment中,稍后可以从这里检索属性。用getProperty()实现的。

    1、深入学习Spring的Environment

    1.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.2、Environment还提供了几个与属性相关的方法

    //获取属性值,找不到抛出异常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处于激活状态:

    • String[] getActiveProfiles():返回激活profile名称的数组;
    • String[] getDefaultProfiles():返回默认profile名称的数组;
    • boolean acceptsProfiles(String... profiles):如果environment支持给定profile的话,就返回true。

    2、属性占位符

      Spring一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到Spring bean中。

      占位符的形式为使用"${}"包装的属性名称,为了使用属性占位符,我们必须配置一个PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer实例,从Spring 3.0开始,推荐使用PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。

    1、在基于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);
        }
    
    }

    2、在基于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表达式语言进行装配

      Spring 3引入了Spring表达式语言(Spring Expression Language,SpEL),它能够以一种强大和简洁的方式将值装配到bean属性和构造器参数中,在这个过程中所使用的表达式会在运行时计算得到值。SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式,存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL expression }


    SpEL拥有很多特性,包括:

    • 使用bean的ID来引用bean;
    • 调用方法和访问对象的属性;
    • 对值进行算术、关系和逻辑运算;
    • 正则表达式匹配;
    • 集合操作。

    1、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-命名空间条目的值。

    2、表示字面值

    使用SpEL来表示整数字面量、浮点数、String值以及Boolean值。

    //表示数值1
    #{1}
    
    //表示浮点值
    #{3.14159}
    
    //表示科学记数法,下面值:98,700
    #{9.87E4}
    
    //表示String类型的字面值
    #{'Hello'}
    
    //表示Boolean类型的值
    #{false}

      在SpEL中使用字面值其实没有太大的意思,只包含字面值情况并没有太大的用处。SpEL表达式是由更简单的表达式组成d的。了解在SpEL中如何使用字面量还是很有用处的,当组合更为复杂的表达式时,你迟早会用到它们。

    3、引用bean、属性和方法

      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()}

    4、在表达式中使用类型(Class对象)

      如果要在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()。

    5、SpEL运算符

      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

    6、计算正则表达式

      当处理文本时,有时检查文本是否匹配某种模式是非常有用的。SpEL通过matches运算符支持表达式中的模式匹配。matches运算符对String类型的文本(作为左边参数)应用正则表达式(作为右边参数)。matches的运算结果会返回一个Boolean类型的值:如果与正则表达式相匹配,则返回true;否则返回false。

    7、计算集合

       1、SpEL中最令人惊奇的一些技巧是与集合和数组相关的。最简单的事情可能就是引用列表中的一个元素了:

     

       2、为了让这个表达式更丰富一些,假设我们要从jukebox中随机选择一首歌:

      3、它还可以从String中获取一个字符。下标基于零开始,也就结果为"s",如下:

     

      4、SpEL还提供了查询运算符(  .?[ ]   ),它会用来对集合进行过滤,得到集合的一个子集。假设你希望得到jukebox中artist属性为Aerosmith的所有歌曲。

    以看到,选择运算符在它的方括号中接受另一个表达式。当SpEL迭代歌曲列表的时候,会对歌曲集合中的每一个条目计算这个表达式。如果表达式的计算结果为true的话,那么条目会放到新的集合中。否则的话,它就不会放到新集合中。

      5、SpEL还提供了另外两个查询运算符:“.^[ ]”和“.$[ ]”,它们分别用来在集合中查询第一个匹配项和最后一个匹配项。

      6、SpEL还提供了投影运算符(.![ ]),它会从集合的每个成员中选择特定的属性放到另外一个集合中。作为样例,假设我们不想要歌曲对象的集合,而是所有歌曲名称的集合。如下的表达式会将title属性投影到一个新的String类型的集合中:

     

    实际上,投影操作可以与其他任意的SpEL运算符一起使用。比如,我们可以使用如下的表达式获得Aerosmith所有歌曲的名称列表:

     

  • 相关阅读:
    POJ 3348 Cows (凸包面积)
    P4126 [AHOI2009]最小割
    P1903 [国家集训队]数颜色
    P3749 [六省联考2017]寿司餐厅
    CF666E Forensic Examination
    P2336 [SCOI2012]喵星球上的点名
    P1117 [NOI2016]优秀的拆分
    P3747 [六省联考2017]相逢是问候
    CF1062F Upgrading Cities
    P3243 [HNOI2015]菜肴制作
  • 原文地址:https://www.cnblogs.com/TvvT-kevin/p/10018001.html
Copyright © 2011-2022 走看看