高级装配 —— 如何在运行时计算要注入到 bean 属性中的值?

依赖注入,通常是将一个 bean 引用注入到另一个 bean 的属性或构造器参数中。它通常指的是将一个对象与另一个对象进行关联
但是装配的另一个方面指的是将一个值注入到 bean 的属性或者构造器参数中

前面所讲的关于值的注入,基本上都硬解码。即值都是写死的。但是有时候,我们希望这些值在运行时再确定

Q1:Spring 提供了哪些在运行时求值的方式?
A1:两种方式:

  • 属性占位符(较为简单)
  • Spring 表达式语言(SpEL)(更为强大)
Q:如何注入外部的值?

A:Spring 中,处理外部值的最简单方式就是声明属性源并通过 Spring 的 Environment 来检索属性。

package com.myapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * 使用外部的属性来装配 BlankDisc bean
 */
@Configuration
// 声明属性源,这个属性文件会加载到 Spring 的 Environment 中,之后可以从这里检索属性
@PropertySource("classpath:./com/myapp/app.properties")
public class ExpressiveConfig {
    @Autowired
    Environment environment;
    @Bean
    public BlankDisc disc() {
        // 检索属性值
        return new BlankDisc(environment.getProperty("disc.title"), environment.getProperty("disc.artist"));
    }
}

app.properties

disc.title=this is title
disc.artist=this is artist

深入学习 Spring 的 Environment
获取属性值的方法:

  • String getProperty(String key);
  • String getProperty(String key, String defaultValue); // 属性不存在时,使用默认值
  • T getProperty(String key, Class targetType);
  • T getProperty(String key, Class targetType, T defaultValue); //得到 T 类型的属性值或默认值

当使用 getProperty() 方法的时候,没有指定默认值,并且这个属性没有定义的话,获取到的值是 null。如果希望这个属性必须要定义,那么可以使用 getRequiredProperty() 方法。

如果想检查一下某个属性是否存在的话,可以使用 containsProperty() 方法。

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

Environment 还提供了一些方法来检查哪些 profile 处于激活状态:

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

通常,不会频繁的使用 Environment 的方法,但是在 Java 配置中装配 bean 的时候,非常方便。

Spring 一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到 Spring bean 中。在 Spring 装配中,占位符的形式为使用 “${…}” 包装的属性名称。在XML中解析 BlankDisc 构造器参数。

<bean id="sgtPeppers" class="soundsystem.BlankDisc"
          c:title="${disc.title}"
          c:artist="${disc.artist}"/>

如果我们依赖于组件扫描自动装配来创建和初始化应用组件的话,那么就没有指定占位符的配置文件或类了。这种情况下,可以使用 @Value 注解

    public BlankDisc(@Value("${disc.title}") String title,@Value("${disc.artist}") String artist) {
        this.title = title;
        this.artist = artist;
    }

为了使用占位符,我们必须配置一个PropertyPlaceholderConfigurer bean 或 PropertySourcesPlaceholderConfigurer bean(Spring 3.1 开始,推荐使用,因为它能基于 Spring Environment 及其属性源来解析占位符

    /**
     * 在 Java 中配置 PropertySourcesPlaceholderConfigurer
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }

或在 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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="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.xsd">

    <!-- 需要声明 Spring context命名空间 -->
    <!-- 使用 XML 配置 PropertySourcesPlaceholderConfigurer bean-->
    <context:property-placeholder/>

</beans>

Spring 3 引入了 Spring 表达式语言(SpEL),它能以一种强大和简介的方式将值装配到 bean 属性和构造器中,在这个过程中所使用的表达式会在运行时计算得到值。

Q:如何使用 Spring 表达式语言进行装配?

首先了解 SpEL 拥有的特性:

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

Q1:SpEL 有哪些样例?
A1: ①、SpEL 表达式要放到 “#{…}” 之中;
②、#{T(System).currentTimeMillis()} 得到当前时间的毫秒数。T() 表达式会将 java.lang.System 视为 Java 中对应的类型;
③、#{sgtPeppers.artist} 得到 ID 为 sgtPeppers 的 bean 的 artist 属性;
④、#{systemPropertise[‘disc.title’]} 通过 systemProperties 对象引用系统属性;
等部分基础样例。

Q2:如何在 bean 装配时使用 SpEL 表达式
A2: 从系统属性中获取专辑名称和艺术家的名字

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

或者在 XML 配置中,你可以将 SpEL 表达式传入 property 或 constructor-arg 元素的 value 属性中,或将其作为 p- 命名空间或 c-命名空间条目的值。

    <!-- 构造器参数通过 SpEL 表达式设置 -->
    <bean id="sgtPeppers" class="soundsystem.BlankDisc"
          c:title="#{systemProperties['disc.title']}"
          c:artist="#{systemProperties['disc.artist']}"/>

Q3:SpEL 支持哪些基础表达式?
A3:
①、表示字面值:#{3.14159}、#{9.87E4} // 得到98700、#{‘Hello’}、#{false};

②、引用 bean、属性和方法:

  • #{sgtPeppers} //引用 sgtPeppers bean;
  • #{sgtPeppers.artist} //引用 sgtPeppers bean 的属性;
  • #{artistSelector.selectArtist()} // 引用 artistSelector 的 selectArtist() 方法;
  • #{artistSelector.selectArtist().toUpperCase()} // 如果 selectArtist() 方法返回 String,则可以调用 toUpperCase() 方法;
  • #{artistSelector.selectArtist()?.toUpperCase()} // 为了避免出现 NullPointerException,我们使用了类型安全的运算符;如果 artistSelector.selectArtist() 返回的是 null 的话,SpEL 将不会调用 toUpperCase() 方法,表达式返回值是 null;

③、在表达式中使用类型:如果要在 SpEL 中访问类作用域的方法常量的话,要依赖 T() 这个关键的运算符。
如:T(java.lang.Math) // 结果是一个 class 对象,代表 java.lang.Math;
T(java.lang.Math).PI // 使用 PI 值、T(java.lang.Math).random() // 得到 0-1 之间的随机数。

④、SpEL 运算符:(用在 SpEL 表达式的值上)
以下为用来操作表达式值的 SpEL 运算符
这里写图片描述

简单的样例:
#{2 * T(java.lang.Math).PI * circle.radius} // 计算圆的周长;
#{T(java.lang.Math).PI * circle.radius ^ 2} // 计算圆的面积;#{disc.title+’ by ‘+disc.artist}

比较运算符:
#{counter.total == 100} 或者 #{counter.total eq 100} 表达式的结果是个 Boolean 值。

三元运算符:
#{scoreboard.score > 1000 ? “Winner!” : “Loser”}
常用的场景:检查 null 值#{disc.title ? : ‘titleValue’}

⑤、计算正则表达式:当处理文本时,检查文本是否匹配某种模式是非常有用的。SpEL 通过 matches 运算符支持表达式中的模式匹配。matches 运算符对 String 类型的文本(作为左边参数)应用正则表达式(作为右边参数)。matches 的运算结果会返回一个 Boolean 类型的值。
#{admin.email matches ‘[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.com’}

⑥、计算集合:
#{jukebox.songs[4].title} // 计算 songs 集合中第五个(基于零开始)元素的 title 属性,这个集合来源于 ID 为 jukebox bean;
#{jukebox.songs[T(java.lang.Math).random() * jukebox.songs.size()].title} // 从 jukebox 中随机选择一首;
#{‘this is a test’[3]} // 引用 String 中的 第四个字符 ‘s’ ;

SpEL 还提供了查询运算符(.?[]),它会用来对集合进行过滤,得到集合的一个子集:
#{jukebox.songs.?[artist eq ‘Aersomith’]} // 得到 jukebox 中 artist 属性为 Aersomith 的所有歌曲。

SpEL 还提供了另外两个查询运算符:“.^[]”“.$[]”,它们分别用来在集合中查询第一个匹配项最后一个匹配项
#{jukebox.songs.^[artist eq ‘Aersomith’]} //查找列表中第一个 artist 属性为 Aersomith 的歌曲;

最后,SpEL 还提供了投影运算符(.![]),它会从集合的每个成员中选择特定的属性放到另外一个集合中:
#{jukebox.songs.![title]} // 将 title 属性投影到一个新的 String 类型的集合中;
#{jukebox.songs.?[artist eq ‘Aersomith’].![title]} // 获得 Aersomith 所有歌曲的名称列表

总结

①、学习了 Spring profile,它解决了 Spring bean 要跨各种部署环境的通用问题。它是在运行时条件化创建 bean 的一种方式。
②、Spring 4 提供了一种更为通用的方式:结合使用 @Conditional 注解Spring Condition 接口的实现,能够实现条件化地创建 bean。通过这种方式,能够声明某些 bean 的创建与否要依赖于给定条件的输出结果
③、两种解决自动装配歧义性的方法:首选bean限定符。学会了如何将一组可选的自动装配 bean ,借助限定符将其范围缩小到只有一个符合条件的 bean ? 如何创建自定义的限定符注解?
④、Spring 能让 bean 以单例、原型、请求作用域或会话作用域的方式来创建。学会了如何在声明请求作用域或会话作用域的 bean 时,创建作用域代理?基于的代理和基于接口的代理。
⑤、学会了 Spring 表达式语言,它能够在运行时计算要注入到 bean 属性中的值。

上一篇:高级装配 —— 如何在不同的作用域中声明 bean?
下一篇:面向切面的 Spring —— 什么是面向切面编程?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值