项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821

(一)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);// return "Hello World!";
}

SpringBoot学习:获取yml和properties配置文件的内容的更多相关文章

  1. SpringBoot学习:获取yml和properties配置文件的内容(转)

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...

  2. SpringBoot学习:获取yml和properties配置文件的内容(转)

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...

  3. Springboot 之 解决IDEA读取properties配置文件的中文乱码问题

    问题描述 当在.properties的配置文件中有中文时,读取出来的总是乱码.比如我的application.properties配置文件的内容如下: server.port=9090 test.ms ...

  4. SpringBoot学习:读取yml和properties文件的内容

    一.在SpringBoot实现属性注入: 1).添加pom依赖jar包: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://m ...

  5. Springboot 获取yml、properties参数

    获取properties或yml文件的配置数据(两种方法)(默认的application文件或者自定义的yml和properties) 1.使用@Value()注解 1.1 配置数据 如:在prope ...

  6. SpringBoot 项目不加载 application.properties 配置文件

    起因:新安装的idea第一次运行springboot项目报url错误(Failed to configure a DataSource: 'url' attribute is not specifie ...

  7. SpringBoot学习遇到的问题(1) - 配置文件有日志的debug模式等配置项,为什么不起作用

    这个问题困扰我近乎两天,通过查找N多资料后终于解决,写下来共享给大家. logging.level.root=DEBUG ... 一系列的日志配置项,都不起作用的原因是springboot启动加载不到 ...

  8. SpringBoot中 application.yml /application.properties常用配置介绍

    # Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 10444 ser ...

  9. yml和properties配置文件区别

    我们可以观察到的格式就是yml文件是梯级呈现的,我们应该注意一下几个方面: 1>在properties文件里面的 “ .”  连接在yml文件里面全部换成 ":" 进行连接, ...

随机推荐

  1. 从windows到linux的换行转换工具dos2unix

    同学们也许知道,windows中的文本文件的换行符是"\r\n",而linux中是"\n".由于换行符的不同,所以有的时候会发生一些莫名其妙的状况.至于具体什么 ...

  2. mac zsh 快捷定位文件

    brew install zsh vim ~/.zshrc plugins=(git autojump zsh-autosuggestions zsh-syntax-highlighting yarn ...

  3. libconfig C++ 学习笔记

    1. C++API 头文件 #include <libconfig.h++> ,命名空间:using namespace libconfig; 2.多线程使用问题: (1)libconfi ...

  4. 关于PHP上传文件失败但是找不到原因的问题?

    确定上传文件的前后台代码都准确无误后,可以考虑是不是PHP配置文件中限制了上传文件的大小 1.找到根目录中的PHP文件夹 2.点击当前正在使用的的PHP版本 3.搜索php.ini 4.打开 phpF ...

  5. windows8安装msi软件提示2503错误的解决办法

    windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...

  6. Mybatis 和Spring整合之mapper代理开发

    F:\1ziliao\mybatis\代码 1.1 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...

  7. @SuppressWarnings注解用法详解

    @SuppressWarnings注解用法详解 今天来谈谈@SuppressWarnings注解的作用. J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条 ...

  8. 最简单的Servlet继承HttpServlet查询数据库登录验证

    <%-- Created by IntelliJ IDEA. User: yunqing Date: 2017-12-06 Time: 9:11 To change this template ...

  9. RabbitMQ之五种消息模型

    首先什么是MQ MQ全称是Message Queue,即消息对列!消息队列是典型的:生产者.消费者模型.生产者不断向消息队列中生产消息,消费者不断的从队列中获取消息.因为消息的生产和消费都是异步的,而 ...

  10. [转] 有关java中两个整数的交换问题

    转载申明:本文主要是用于自己学习使用,为了完善自己的只是框架,没有任何的商业目的. 原文来源:有关Java中两个整数的交换问题 如果侵权,麻烦告之,立刻删除. 在程序开发的过程,要交换两个变量的内容, ...