SpringBoot获取yml和properties配置文件的内容
作者:彩虹过后的羽翼 发布时间:2022-12-02 18:26:08
标签:SpringBoot,yml,properties
本文实例为大家分享了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实现商城秒杀系统,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 在网站开发中经常遇到级联数据的展示,比如选择城市的时候弹出的省市县选择界面。很多前端制作人员习惯于从JSON中而不是从数据库中获
- 在往常的 HTTP 调用中,一直都是使用的官方提供的 RestTemplate 来进行远程调用,该调用方式将组装代码冗余到正常业务代码中,不
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- 前言 开始始学习android,对android的启动模式没有什么了解,就使用了时间判断是否重复点击了两次按钮,启动另外的activity界
- strcpy函数详解如下1.函数介绍1.1.函数接口char * __cdecl strcpy(char * dst, const char
- 本文主要介绍了idea中同一SpringBoot项目多端口启动,具体如下:现在已经有一个在跑着使用的默认端口 8080选中1,点击2.这个时
- 什么是面向对象Java语言是一个纯面向对象的语言,面向对象的语言不仅只有Java,包括C++,PHP等面向对象的编程思想简称 OOP(Obj
- 前言本文基于 RxJava 和 Retrofit 库,设计并实现了一种用于大文件分块上传的工具,并对其进行了全面的拆解分析。抛砖引玉,对同样
- 这篇文章主要介绍了JDBC自定义连接池过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- SearchView是搜索框组件,它可以让用户在文本框里输入文字,通过 * 取得用户的输入,当用户点击搜索时, * 执行实际的搜索。本文就为
- java 基础之JavaBean属性命名规范问题JavaBean属性名要求:前两个字母要么都大写,要么都小写下面我们来找找如果不遵循这个规范
- 本文实例讲述了Android编程自定义AlertDialog样式的方法。分享给大家供大家参考,具体如下:开发的时候,通常我们要自定义Aler
- 本文实例为大家分享了Android实现二级列表购物车功能的具体代码,供大家参考,具体内容如下MainActivity:package com
- 写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址代码量不多,很容易看懂/** * 作者:叶应是叶 * 时间:2
- 模型对象的作用主要是保存数据,可以借助它们将数据带到前端。常用的模型对象有以下几个:ModelAndView(顾名思义,模型和视图,既可以携
- 文档地址https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring
- 问题(1)重入锁是什么?(2)ReentrantLock如何实现重入锁?(3)ReentrantLock为什么默认是非公平模式?(4)Ree
- 本文实例为大家分享了Opencv实现画笔功能的具体代码,供大家参考,具体内容如下#include<iostream>#inclu
- 本文实例为大家分享了Android列表时间轴展示的具体代码,供大家参考,具体内容如下实现的效果图如下:实现的方式是利用recycleview