SpringBoot获取yml和properties配置⽂件的内容本⽂实例为⼤家分享了SpringBoot获取yml和properties配置⽂件的具体代码,供⼤家参考,具体内容如下
(⼀)yml配置⽂件:
<!-- ⽀持 @ConfigurationProperties 注解 -->
<!-- mvnrepository/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
</dependency>
在l⽂件中加上:
#⾃定义的属性和值
myYml:
simpleProp: simplePropValue
arrayProps: 1,2,3,4,5
listProp1:
- name: abc
value: abcValue
- name: efg
value: efgValue
listProp2:
递归算法的解题步骤-
config2Value1
- config2Vavlue2
mapProps:
key1: value1
key2: value2
使⽤⼀个java类获取yml⽂件的内容:
package figuration;
import org.t.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
网游之大神非要千里送
import java.util.List;
import java.util.Map;
/**
* 加载yaml配置⽂件的⽅法
* Created by sun on 2017-1-15.
* spring-boot更新到1.5.2版本后locations属性⽆法使⽤
* @PropertySource注解只可以加载proprties⽂件,⽆法加载yaml⽂件
* 故现在把数据放到l⽂件中,spring-boot启动时会加载
*/
@Component
//@ConfigurationProperties(locations = {"classpath:l"},prefix = "myProps")
@ConfigurationProperties(prefix = "myYml")
public class YmlConfig {
String simpleProp;
private String[] arrayProps;
private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1⾥⾯的属性值
private List<String> listProp2 = new ArrayList<>(); //接收prop2⾥⾯的属性值
private Map<String, String> mapProps = new HashMap<>(); //接收prop1⾥⾯的属性值
public String getSimpleProp() {
return simpleProp;
}
//String类型的⼀定需要setter来接收属性值;maps, collections, 和 arrays 不需要
public void setSimpleProp(String simpleProp) {
this.simpleProp = simpleProp;
}
public String[] getArrayProps() {
return arrayProps;
}
public void setArrayProps(String[] arrayProps) {
this.arrayProps = arrayProps;
}
public List<Map<String, String>> getListProp1() {
return listProp1;
}
public void setListProp1(List<Map<String, String>> listProp1) {
this.listProp1 = listProp1;
}
public List<String> getListProp2() {
return listProp2;
}
public void setListProp2(List<String> listProp2) {
this.listProp2 = listProp2;
}
public Map<String, String> getMapProps() {
return mapProps;
}
public void setMapProps(Map<String, String> mapProps) {
this.mapProps = mapProps;
}
}
通过依赖注⼊就可以获取该对象:
@Autowired
private YmlConfig config;
⽅法内获取值:
ObjectMapper objectMapper = new ObjectMapper();
/
/测试加载yml⽂件
System.out.println("simpleProp: " + SimpleProp());
System.out.println("arrayProps: " + objectMapper.ArrayProps()));
System.out.println("listProp1: " + objectMapper.ListProp1()));
System.out.println("listProp2: " + objectMapper.ListProp2()));
System.out.println("mapProps: " + objectMapper.MapProps()));
(⼆)properties配置⽂件:
使⽤@PropertySource注解加载配置⽂件,该注解⽆法加载yml配置⽂件。使⽤@Value注解获得⽂件中的参数值package figuration;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.PropertySource;
import t.support.PropertySourcesPlaceholderConfigurer;
/**
* 加载properties配置⽂件,在⽅法中可以获取
* abc.properties⽂件不存在,验证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这个bean,
// 这个bean主要⽤于解决@value中使⽤的${…}占位符。
// 假如你不使⽤${…}占位符的话,可以不使⽤这个bean。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
//获取properties⽂件参数值有两种⽅法,⼀种获得Environment 的对象,第⼆种就是@Value注解
ahover是什么意思>contacted是什么意思
@Autowired
private Environment env;
properties是什么文件
@Value("${age}")
String name;
@RequestMapping("/")
@ResponseBody
String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {    logger.info("测试通过");
ObjectMapper objectMapper = new ObjectMapper();
//测试加载yml⽂件
System.out.println("simpleProp: " + SimpleProp());
gridview控件修改System.out.println("arrayProps: " + objectMapper.ArrayProps()));
System.out.println("listProp1: " + objectMapper.ListProp1()));
System.out.println("listProp2: " + objectMapper.ListProp2()));
System.out.println("mapProps: " + objectMapper.MapProps()));
//测试加载properties⽂件
System.out.Property("name"));//孙凯
System.out.Property("abc"));//null
System.out.println(name);//26
return "Hello World!";
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。