SpringBoot讀取application.properties文件


https://www.cnblogs.com/duanxz/p/3469511.html

 

springboot 配置文件 .properties和.yml的寫法區別

例如 :    redis配置的properties或yml文件,如下:

  1. spring.redis.cluster.nodes[0]=192.168.0.1:6379  
  2. spring.redis.cluster.nodes[1]=192.168.0.2:6379  
  3. 或  
  4. spring:  
  5.    redis:  
  6.       cluster:  
  7.          nodes:  
  8.             - 192.168.0.1:6379  
  9.             - 192.168.0.2:6379  

spring boot允許你自定義一個application.properties文件,然后放在以下的地方,來重寫spring boot的環境變量或者定義你自己環境變量

  1. 當前目錄的 “/config”的子目錄下
  2. 當前目錄下
  3. classpath根目錄的“/config”包下
  4. classpath的根目錄下

1點和2點適合在生產環境下,例如,打包成可執行的jar包

這里寫圖片描述

這里要注意,“當前目錄”是指demo.jar包的目錄下,要使配置文件生效,在使用Java -jar demo.jar的命令時,必須先路由到demo.jar包的路徑下,再使用其命名,

這里寫圖片描述

3點和4點適合在開發環境下

這里寫圖片描述

如果同時在四個地方都有配置文件,配置文件的優先級是從1到4。

使用配置文件之后,spring boo啟動時,會自動把配置信息讀取到spring容器中,並覆蓋spring boot的默認配置,那么,我們怎么來讀取和設置這些配置信息呢

1.通過命令行來重寫和配置環境變量,優先級最高,例如可以通過下面的命令來重寫spring boot 內嵌tomcat的服務端口,注意“=”倆邊不要有空格

java -jar demo.jar --server.port=9000
  • 1
  • 1

如果想要設置多個變量怎么辦,可以已json的格式字符串來設置

java -jar demo.jar --spring.application.json='{"foo":"bar"}'

2.通過@value注解來讀取

@RestController @RequestMapping("/task") public class TaskController { @Value("${connection.remoteAddress}") private String address; @RequestMapping(value = {"/",""}) public String hellTask(@Value("${connection.username}")String name){ return "hello task !!"; } }

 

3.通過Environment接口來獲取,只需要把接口注進去即可

@RestController @RequestMapping("/task") public class TaskController { @Autowired Environment ev ; @Value("${connection.remoteAddress}") private String address; @RequestMapping(value = {"/",""}) public String hellTask(@Value("${connection.username}")String name){ String password = ev.getProperty("connection.password"); return "hello task !!"; } }

 

4.可以自定義一個工具類,來獲取,這種方式關鍵在於讀取配置文件信息,適合自定義的配置信息,spring 容器默認的配置信息會讀不到

@Component public class SystemConfig { private static Properties props ; public SystemConfig(){ try { Resource resource = new ClassPathResource("/application.properties");// props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取屬性 * @param key * @return */ public static String getProperty(String key){ return props == null ? null : props.getProperty(key); } /** * 獲取屬性 * @param key 屬性key * @param defaultValue 屬性value * @return */ public static String getProperty(String key,String defaultValue){ return props == null ? null : props.getProperty(key, defaultValue); } /** * 獲取properyies屬性 * @return */ public static Properties getProperties(){ return props; } } //用的話,就直接這樣子 String value = SystemConfig.getProperty("key");

 

5.可以利用${…}在application.properties引用變量

myapp.name=spring myapp.desc=${myapp.name} nice

 

6.可以在application.properties配置隨機變量,利用的是RandomValuePropertySource類

my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}

 

簡單的配置文件的使用就先寫到這里,再看看其他高級用法,如Profiles還有@ConfigurationProperties

=================================

SpringBoot讀取application.properties文件,通常有3種方式

1. @Value  例如: 

@Value("${spring.profiles.active}")

private String profileActive;------相當於把properties文件中的spring.profiles.active注入到變量profileActive中

2. @ConfigurationProperties  例如:

 

@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;

}

其他類中使用時,就可以直接注入該TestProperties 進行訪問相關的值

3. 使用Enviroment   例如:

private Enviroment env;

env.getProperty("test.url");

而env方式效率較低

 

注:@ConfigurationProperties也可用於其他.properties文件,只要locations指定即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM