美文网首页
Spring高级配置之运行时注入

Spring高级配置之运行时注入

作者: 郭之源 | 来源:发表于2016-08-31 09:32 被阅读3136次

    之前,我们设置Bean的属性值时,采用的都是硬编码的形式。比如,在定义BlankDisc时:

    @Bean
    public BlankDisc blankDisc(){
        
        return new BlankDisc("This is BlankDisc", "The Beatles");
    }
     
    

    与之类似,采用XML的方式也是硬编码:

    <bean id = "blankDisc" class = "com.inject.service.BlankDisc"
    c:_title = "This id BlankDisc"
    c:_artist = "This Beatles" />     
    

    硬编码的坏处想必大家都知道,所以我们就想能不能让这些值在程序运行时再确定。其实Spring已经提供了两种方式实现运行时注入的功能:

    • 属性占位符(Property planceholder)
    • Spring 表达式语言(SpEL)

    1.使用属性占位符注入外部的值

    @Autowired
    Environment env;
    
    public BlankDisc blanKDiscBean2(){
        return new BlankDisc(
                env.getProperty("disc.title"),
                env.getProperty("disc.artist"));
    }
    

    1.1深入学习Spring的Environment

    Environment的getProperty()方法有四个重载方法:

    • String getProperty(String key)
    • String getProperty(String key,String defaultValue)
    • T getProperty(String key, Class<T> type)
    • T getProperty(String key, Class<T> type,T defaultValue)

    前两种形式的getProperty()方法都是返回String类型的值。后两种重载方法不是直接返回字符串,而是将获取到的值转化为指定类型的值,比如获取连接池中维持连接的总数量:

    int connectionCount = env.getProperty("db.connoctionCount",Integer.class,30);
    

    在使用getProperty()获取属性时,如果这个属性没有定义,那他获取的是null,如果我们不想让这样的情况发生,可以使用getRequiredProperty()方法:

    //必须定义参数
    env.getRequiredProperty("disc.title"),
    env.getRequiredProperty("disc.artist"));
    

    如果disc.title或disc.arist没有定义的话,将会抛出IllegalStateException异常。
    如果你想检查某个属性是否存在,那么可以调用containsProperty()方法:

    boolean titleExists = env.containsProperty(“disc.title”);
    

    如果你想将属性解析为类的话,可以使用getPropertyAsClass()方法:

    Class<BlankDisc> blankDisc = env.getPropertyAsClass("disc.class",BlankDisc.class);
    

    除了属性相关的功能外,Environment还提供了一些方法来检查哪些profile处于激活状态:

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

    1.2解析属性占位符

    在Spring装配中,占位符的形式为“${...}”包装的属性名称。下面我们使用属性占位符在XML中解析tomcat连接池bean的属性:

        <!-- 数据源配置, 使用Tomcat JDBC连接池 -->
        <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
            <!-- Connection Info -->
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
    
            <!-- Connection Pooling Info -->
            <property name="maxActive" value="${jdbc.pool.maxActive}"/>
            <property name="maxIdle" value="${jdbc.pool.maxIdle}"/>
        </bean>
    

    要使用占位符,需要配置PropertySourcesPlaceholderConfigurer bean:

    @Bean
    public static PropertySourcesPlaceholderConfigurer placehoderConfigurer(){
        
        return new PropertySourcesPlaceholderConfigurer();
    }
    

    在Xml配置要使用到命名空间中的<context:property-placeholder>元素,它可以为我们生成PropertySourcesPlaceholderConfigurer bean:

    <context:property-placeholder ignore-unresolvable="true"
                       location="classpath*:/application.properties,
                       classpath*:/application.local.properties" file-encoding="UTF-8"/>
    

    2.使用Spring表达式语言进行配置

    Spring 3引入了Spring表达式(Spring Excpression Language,SpEL),他能够以一种强大和简洁的方式将值装配到bean属性和构造器参数中,这个过程中所使用的表达式会在运行时计算得到值。SpEL的特性有以下几点:

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

    2.1SpEl表达式样例

    格式 说明
    #{1} 得到的值就是1
    #{T(System).currentTimeMillis() 获取当前时间的毫秒值,T()表达式会将java.lang.System视为java中对应的类型,通过.方法调用对应的静态方法
    #{blankDisc.title} 获取ID为blankDisc bean的属性值
    #{systemPropertys['disc.title']} 通过systemPropertys对象引用系统属性

    上面几个只是几个基本的SpEl样例,后面我们会着重介绍,下面我们先看看使用SpEL表达式装配bean的属性:

    public BlankDisc(
                @Value("#{systemPropertys['disc.title']}") String title, 
                @Value("#{systemPropertys['disc.artist']}") String artist){
            this.title = title;
            this.artist = artist;
        }
    

    上例中使用到@Value注解,在注解中设置SpEL表达式。上面我们学习了几个简单的样例,也学习了如何将SpEL表达式解析得到的值注入到bean中,那么我们来继续学习一下SpEl表达式支持的基础表达式吧。

    2.2表示字面值

    使用SpEL字面量样式可以表示整数、浮点数、String以及Boolean类型的值:

    #{1}
    #{3.14159}
    #{9.87E4}
    #{'Hello'}
    #{false}
    

    2.3引入bean属性和方法

    SpEL能够通过ID引入其他的bean、bean的属性和bean的方法:

    #{blankDisc}
    #{blankDisc.title}
    #{blankDisc.sayHello()}
    

    如果还可以对方法的返回值做处理,直接调用返回值类型的属性或方法,假设sayHello()返回值是String类型:#{blankDisc.sayHello().toUpperCase()},这样就可以获取返回值的大写字母形式。
    但如果返回值是null,调用它的方法就会报空指针异常,为了预防这种情况,可以使用 ? 判断获得返回值是否为null:#{blankDisc.sayHello()?.toUpperCase()}如果sayHello()方法返回值是null,SpEl将不会调用toUpperCase()方法,SpEL的返回值是null。

    2.4在表达式中使用类型

    如果SpEL表达式访问类作用域的方法或常量时,要使用到T()这个关键的运算符:#{T(java.lang.Math)}运算符的结果是一个Class对象,它代表了java.lang.Math。通过它我们可以访问Math的静态方法和常量。

    #{T(java.lang.Math).PI}
    #{T(java.lang.Math).random()}
    

    2.5SpEL运算符

    SpEL提供了多个运算符,这些运算符可以使用到SpEL表达式上:

    运算符类型 运算符
    算术运算符 +、- 、*、/ 、%、^
    比较运算符 <、>、 = 、<=、 >=、 lt、 gt、 eq、 ge
    条件运算符 ?:(ternary)、 ?:(Elvis)
    逻辑运算符 and、 or、not、
    正则表达式 matches

    2.6计算正则表达式

    SpEL通过matches运算符支持表达式中的模式匹配,匹配成功返回true,否则否则返回false,下面我们判断邮箱是否符合正则表达式:

    #{admin.email matches '[a-zA-Z0-9.-%+-]+@[a-zA-Z0-9.-]+\\.com'}
    

    2.7计算集合

    使用SpEL引用列表中的一个元素:#{jubox.songs[4].title},获取列表的第5个元素(基于0开始),[] 是从集合或数组中按照索引取元素。
    SpEL提供了查询运算符(.?[]),他会用来对集合进行过滤,得到集合的一个子集。下面我们将Aerosmith的歌曲过滤出来:

    #{jukebox.songs.?[artist eq 'Aerosmith']}
    

    .^[]查询第一个匹配项
    .$[]查询最后一个匹配项

    投影运算符(.![]),将集合对象特定的字段投影到另一个集合中,下面我们将歌曲的名称投影到另一个String类型的集合中:

    #{jukebox.songs.![title]}
    

    我们还可以过滤要投影的歌曲,下面我们获取Aerosmith歌曲的title:

    #{jukebox.songs.?[artist eq 'Aerosmith'.![title]]}
    

    自此运行时注入的全部内容已经全部介绍完啦。终于完了啦,下个月我们开始学习Spring的Aop机制,很期待吧,come on!

    相关文章

      网友评论

          本文标题:Spring高级配置之运行时注入

          本文链接:https://www.haomeiwen.com/subject/nqsxettx.html