chapter03_高级装配_3_运行时值注入

  • 目标:避免硬编码,通过运行时读取配置文件的值确定Bean的构造器或setter参数,而非在Bean的类中写硬编码代码,实现解耦

  • 运行时求值的方式

    (1) 从外部 properties文件直接读

    (2) 属性占位符 ${ … }

    (3) Spring EL #{ … }

  • 属性文件

    (1) 属性文件放在 src/main/resources 目录下

    示例

      disc.title=Sgt. Peppers Lonely Hearts Club Band
      disc.artist=The Beatles
    

    (2) 获取属性文件: @PropertySource注解

    示例(classpath就是src/main/resources目录)

      @Configuration
      @PropertySource("classpath:com/soundsystem/app.properties")
      public class EnvironmentConfig {
    
          @Autowired
          Environment env;
    
          @Bean
          public BlankDisc blankDisc() {
    
              return new BlankDisc(
                  env.getProperty("disc.title"),
                  env.getProperty("disc.artist"));
          }
      }
    
  • 直接读取

    使用Environment对象(可以Autowired自动装配)的getProperty()方法

    示例见上一个例子

  • 属性占位符

    (1) Java Config 配置没成功过,所以只说 xml配置

    (2) 头部添加xml格式信息的uri

    示例

      <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-4.0.xsd">
    

    (3) 使用context:property-placeholder的location属性配置properties文件的位置

    示例

      <context:property-placeholder location="com/soundsystem/app.properties"/>
    

    (4) 使用属性占位符替换变量value

    示例

      <bean class="com.soundsystem.BlankDisc">
          <constructor-arg value="${disc.title}"/>
          <constructor-arg value="${disc.artist}"/>
      </bean>
    
  • Spring EL

    (1) Spring EL具有的能力

    1 使用bean的id来引用bean

    2 调用bean的方法和属性(private也行)

    3 算术、关系、逻辑运算

    4 正则表达式

    5 集合操作

    (2) Java Config 目前没配置成功过,所以使用xml说明

    (3) 引用bean、属性和方法

    示例

      <bean class="com.soundsystem.BlankDisc" id="blankDisc">
          <constructor-arg value="${disc.title}"/>
          <constructor-arg value="${disc.artist}"/>
      </bean>
    
      <bean class="com.soundsystem.NewBlankDisc">
          <constructor-arg value="#{blankDisc.title}"/>
          <constructor-arg value="#{blankDisc.getArtist()}"/>
      </bean>
    

    (4) 字面量

    示例

      #{3.14}             double
      #{"Hello"}          String
      #{false}            boolean
    

    (5) 使用类型

    通过T()运算符,可以将类转化为一个class对象,从而使用它的静态方法和常量

    示例

      #{T(java.lang.System).currentTimeMillis()}
    

    (6) 运算

    示例

      #{2.0 * T(java.lang.Math).PI * circle.radius * circle.getRadius()}
    

    (7) 正则表达式

    matches运算符,返回Boolean类型

    示例

      #{user.email matches '[a-zA-Z0-9.]'}
    

    (8) 集合

    [] 运算符可以从集合或数组或字符串中获取第k个元素

    .?[] 运算符类似于HashTable通过key查找value

猜你喜欢

转载自blog.csdn.net/captxb/article/details/87863888