springboot讀取配置文件到靜態工具類

通常我們讀取配置文件可以用@Value註解和@Configuration,@ConfigurationProperties(prefix = "xxx")等註解,但是這種方式是無法把配置讀取到靜態變量的,如果我們想在項目初始化時把配置文件加載到一個工具類,然後通過靜態變量的方式調用的話我們就不能使用這兩種方法。
這時候,我們可以用Environment 來解決。

不廢話了,直接上代碼


import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 
 * @Description: 配置常量類——根據不同的spring-profile加載不同的配置
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
@Component
public class ConfigConstant {
  @Autowired
  private Environment env;
  
  public static String url;
  public static String param;
  

  @PostConstruct
  public void readConfig() {
    url = env.getProperty("config.url");
    param = env.getProperty("config.param");
  }
}

我寫完以後發現有些麻煩,下面是改進的方法,不需要每個配置都去get一下,只需要把配置文件的key與工具類的靜態變量名寫成一樣的即可。

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 
 * @Description: 配置常量類——根據不同的spring-profile加載不同的配置,變量名要與配置文件裏寫的名一致
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
@Component
public class ConfigConstant {
  @Autowired
  private Environment env;
  
  public static String url;
  public static String name;
  
  
  @PostConstruct
  public void readConfig() throws Exception {
    String prefix = "config.";
    Field[] fields = ConfigConstant.class.getFields();
    for(Field field : fields ){
      field.set(null, getProperty(prefix + field.getName()));
    }
  }
  
  private String getProperty(String key) throws UnsupportedEncodingException {
    return new String(env.getProperty(key).getBytes("ISO-8859-1"), "UTF-8");
  }
}

大哥說這樣寫依賴spring, 單測調代碼的時候不方便,所以又寫了一個不依賴spring的版本。


import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 * 
 * @Description: 配置常量類——根據不同的spring-profile加載不同的配置
 *               變量名把配置文件的key中的"."替換成"_"命名
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
public class ConfigConstant {

  public static String CONFIG_URL;
  public static String CONFIG_NAME;

  static {
    try {
      Properties props = new Properties();
      props.load(new InputStreamReader(
          ConfigConstant.class.getClassLoader().getResourceAsStream("application.properties"),
          "UTF-8"));
      String profile = props.getProperty("spring.profiles.active");
      String envFile = "application-" + profile + ".properties";
      Properties envProps = new Properties();
      envProps.load(new InputStreamReader(
          ConfigConstant.class.getClassLoader().getResourceAsStream(envFile), "UTF-8"));
      Field[] fields = ConfigConstant.class.getFields();
      for (Field field : fields) {
        field.set(null, envProps.getProperty(field.getName().replace("_", ".").toLowerCase()));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章