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;
  • 调用方法和访问对象的属性;
  • 对值进行算术、关系和逻辑运算;
  • 正则表达式匹配;
  • 集合操作。

猜你喜欢

转载自www.cnblogs.com/TvvT-kevin/p/10018001.html