美文网首页
Injecting external values

Injecting external values

作者: cdz620 | 来源:发表于2020-03-04 09:41 被阅读0次

    Injecting external values

    运行时动态注入值,有三种方式:

    • Spring Environment
    • property placeholders
    • The Spring Expression Language(SPEL)

    前提条件

    • 配置PropertyPlaceholderConfigurer,作为入口
        <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:*.properties</value>
                </list>
            </property>
            <property name="fileEncoding" value="UTF-8"/>
        </bean>
    
    • 或者使用: <util:properties />

    docs

    1. Spring Environment

    package com.soundsystem;
    
    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;
    
    @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"));
      }
    }
    

    spring’s environment api

    • String getProperty(String key): env.getProperty("disc.title")
    • String getProperty(String key, String defaultValue): env.getProperty("disc.artist", "U2"));
    • T getProperty(String key, Class<T> type): Class<CompactDisc> cdClass =
      env.getProperty("disc.title", String.class);
    • T getProperty(String key, Class<T> type, T defaultValue): int connectionCount =
      env.getProperty("db.connection.count", Integer.class, 30);
    • getRequiredProperty("disc.title"): 不存在抛 IllegalStateException
    • env.containsProperty("disc.title");
    • boolean titleExists = env.containsProperty("disc.title"): 判断是否存在
    • Class<CompactDisc> cdClass =
      env.getPropertyAsClass("disc.class", CompactDisc.class): 解析a property 到一个类里

    spring environment for profile

    • String[] getActiveProfiles(): Returns an array of active profile names
    • String[] getDefaultProfiles()—Returns an array of default profile names
    • boolean acceptsProfiles(String... profiles)—Returns true if the envi-
      ronment supports the given profile(s)

    2. property placeholders

    component-scann 配置方式

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

    xml 配置

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

    3.SpEL

    JavaConfig

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

    xml

    <bean id="sgtPeppers"
          class="soundsystem.BlankDisc"
          c:_title="#{systemProperties['disc.title']}"
          c:_artist="#{systemProperties['disc.artist']}" />
    

    相关文章

      网友评论

          本文标题:Injecting external values

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