當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot学习:读取yml和properties文件的内容
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot学习:读取yml和properties文件的内容
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、在SpringBoot實(shí)現(xiàn)屬性注入:
1)、添加pom依賴jar包;
1 <!-- 支持 @ConfigurationProperties 注解 --> 2 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> 3 <dependency> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-configuration-processor</artifactId> 6 <version>${spring-boot.version}</version> 7 </dependency>?
2)、在yml配置文件中:
1 #pojo屬性注入 2 Mybar: #pojo中的prefix值 3 name: 張三 4 arrs: 趙,錢,孫,李 5 nameList: 6 - name: 劉 7 value: 劉備 8 - name: 張 9 value: 張飛 10 BarNameList: 11 - 早退次數(shù) 12 - 遲到次數(shù) 13 - 曠工天數(shù) 14 map: 15 key1: 曹操 16 key2: 曹丕 17 key3: 曹植3)、pojo通過set、get方法獲取呀,yml中的值
1 package cn.com.venus.oa.pojo; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.boot.context.properties.ConfigurationProperties; 9 import org.springframework.stereotype.Component; 10 11 /** 12 * 加載yaml配置文件的方法 13 * spring-boot更新到1.5.2版本后locations屬性無法使用 14 * @PropertySource注解只可以加載proprties文件,無法加載yaml文件 15 * 故現(xiàn)在把數(shù)據(jù)放到application.yml文件中,spring-boot啟動(dòng)時(shí)會(huì)加載 16 */ 17 @Component 18 @ConfigurationProperties(prefix="Mybar") 19 public class Bar { 20 private String name; 21 22 private String[] arrs; 23 24 private List<Map<String,String>> nameList; 25 26 private List<String> BarNameList; 27 28 private Map<String,String> map; 29 30 public String getName() { 31 return name; 32 } 33 34 //String類型的一定需要setter來接收屬性值;maps, collections, 和 arrays 不需要 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public String[] getArrs() { 40 return arrs; 41 } 42 43 public void setArrs(String[] arrs) { 44 this.arrs = arrs; 45 } 46 47 public Map<String, String> getMap() { 48 return map; 49 } 50 51 public void setMap(Map<String, String> map) { 52 this.map = map; 53 } 54 55 public List<Map<String, String>> getNameList() { 56 return nameList; 57 } 58 59 public void setNameList(List<Map<String, String>> nameList) { 60 this.nameList = nameList; 61 } 62 63 public List<String> getBarNameList() { 64 return BarNameList; 65 } 66 67 public void setBarNameList(List<String> barNameList) { 68 BarNameList = barNameList; 69 } 70 71 72 }?
4)、最終在Controller中執(zhí)行自動(dòng)注入就可以完成yml配置屬性值:
1 @Autowired 2 private Bar bar;?
?
二、properties配置文件:
使用@PropertySource注解加載配置文件,該注解無法加載yml配置文件。使用@Value注解獲得文件中的參數(shù)值
package com.sun.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; /** * 加載properties配置文件,在方法中可以獲取 * abc.properties文件不存在,驗(yàn)證ignoreResourceNotFound屬性 * 加上encoding = "utf-8"屬性防止中文亂碼,不能為大寫的"UTF-8" * Created by sun on 2017-3-30. */ @Configuration @PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"}, ignoreResourceNotFound = true,encoding = "utf-8") public class PropConfig { // PropertySourcesPlaceholderConfigurer這個(gè)bean, // 這個(gè)bean主要用于解決@value中使用的${…}占位符。 // 假如你不使用${…}占位符的話,可以不使用這個(gè)bean。 @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }獲取properties文件參數(shù)值有兩種方法,一種獲得Environment 的對(duì)象,第二種就是@Value注解
1 @Autowired 2 private Environment env; 3 @Value("${age}") 4 String name; 5 6 7 @RequestMapping("/") 8 @ResponseBody 9 String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException { 10 logger.info("測(cè)試通過!!!"); 11 ObjectMapper objectMapper = new ObjectMapper(); 12 //測(cè)試加載yml文件 13 System.out.println("simpleProp: " + config.getSimpleProp()); 14 System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps())); 15 System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1())); 16 System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2())); 17 System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps())); 18 19 //測(cè)試加載properties文件 20 System.out.println(env.getProperty("name"));//孫凱 21 System.out.println(env.getProperty("abc"));//null 22 System.out.println(name);//26 23 24 return "Hello World!"; 25 }?
轉(zhuǎn)載于:https://www.cnblogs.com/tongxuping/p/7207814.html
總結(jié)
以上是生活随笔為你收集整理的SpringBoot学习:读取yml和properties文件的内容的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Multidex实现简要分析
- 下一篇: gradle idea java ssm