本文实例为大家分享了SpringBoot获取yml和properties配置文件的具体代码,供大家参考,具体内容如下

(一)yml配置文件:
pom.xml加入依赖:
<!-- 支持 @ConfigurationProperties 注解 -->
<!-- https://mvnrepository.com/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>
在application.yml文件中加上:
#自定义的属性和值 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 com.sun.configuration;
import org.springframework.boot.context.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文件
* 故现在把数据放到application.yml文件中,spring-boot启动时会加载
*/
@Component
//@ConfigurationProperties(locations = {"classpath:config/myProps.yml"},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: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));
(二)properties配置文件:
使用@PropertySource注解加载配置文件,该注解无法加载yml配置文件。使用@Value注解获得文件中的参数值
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文件不存在,验证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注解
@Autowired
private Environment env;
@Value("${age}")
String name;
@RequestMapping("/")
@ResponseBody
String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
logger.info("测试通过!!!");
ObjectMapper objectMapper = new ObjectMapper();
//测试加载yml文件
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));
//测试加载properties文件
System.out.println(env.getProperty("name"));//孙凯
System.out.println(env.getProperty("abc"));//null
System.out.println(name);//26
return "Hello World!";
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# SpringBoot
# yml
# properties
# 在SpringBoot下读取自定义properties配置文件的方法
# Spring Boot 日志配置方法(超详细)
# SpringBoot + Spring Security 基本使用及个性化登录配置详解
# springboot如何读取配置文件(application.yml)中的属性值
# 详解SpringBoot配置连接池
# spring boot Logging的配置以及使用详解
# spring boot的maven配置依赖详解
# spring boot开发遇到坑之spring-boot-starter-web配置文件使用教程
# Springboot配置doris连接的实现示例
# 加载
# 配置文件
# 不需要
# 你不
# 不存在
# 自定义
# 有两种
# 大家分享
# 主要用于
# 第二种
# 使用这个
# 具体内容
# 大家多多
# 为大
# 就可以
# 新到
# mapProps
# simplePropValue
# abc
# package
相关文章:
定制建站方案优化指南:企业官网开发与建站费用解析
小程序网站制作需要准备什么资料,如何制作小程序?
建站之星IIS配置教程:代码生成技巧与站点搭建指南
无锡营销型网站制作公司,无锡网选车牌流程?
深圳企业网站制作设计,在深圳如何网上全流程注册公司?
建站之星如何通过成品分离优化网站效率?
建站主机类型有哪些?如何正确选型
如何选择香港主机高效搭建外贸独立站?
制作销售网站教学视频,销售网站有哪些?
清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
如何快速登录WAP自助建站平台?
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
建站之星3.0如何解决常见操作问题?
成都网站制作公司哪家好,四川省职工服务网是做什么用?
如何选择服务器才能高效搭建专属网站?
广德云建站网站建设方案与建站流程优化指南
新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
深圳网站制作案例,网页的相关名词有哪些?
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
香港服务器网站卡顿?如何解决网络延迟与负载问题?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
如何快速配置高效服务器建站软件?
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
已有域名建站全流程解析:网站搭建步骤与建站工具选择
历史网站制作软件,华为如何找回被删除的网站?
网站插件制作软件免费下载,网页视频怎么下到本地插件?
青岛网站建设如何选择本地服务器?
大型企业网站制作流程,做网站需要注册公司吗?
武清网站制作公司,天津武清个人营业执照注销查询系统网站?
网站制作大概多少钱一个,做一个平台网站大概多少钱?
网站制作员失业,怎样查看自己网站的注册者?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
广州网站建站公司选择指南:建站流程与SEO优化关键词解析
C#如何序列化对象为XML XmlSerializer用法
,石家庄四十八中学官网?
如何快速生成可下载的建站源码工具?
如何选择高效稳定的ISP建站解决方案?
如何基于云服务器快速搭建网站及云盘系统?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
Android滚轮选择时间控件使用详解
全景视频制作网站有哪些,全景图怎么做成网页?
实例解析Array和String方法
在线教育网站制作平台,山西立德教育官网?
定制建站哪家更专业可靠?推荐榜单揭晓
如何在阿里云部署织梦网站?
定制建站流程步骤详解:一站式方案设计与开发指南
javascript中对象的定义、使用以及对象和原型链操作小结
*请认真填写需求信息,我们会在24小时内与您取得联系。