项目下载地址: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;</br>

@RequestMapping(</span>"/"<span style="color: #000000">)</br>
@ResponseBody</br>
String home(HttpServletRequest req) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> JsonProcessingException, UnsupportedEncodingException {</br>
logger.info(</span>"测试通过!!!"<span style="color: #000000">);</br>
ObjectMapper objectMapper </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> ObjectMapper();</br>
</span><span style="color: #008000">//</span><span style="color: #008000">测试加载yml文件</span></br>
System.out.println("simpleProp: " +<span style="color: #000000"> config.getSimpleProp());</br>
System.out.println(</span>"arrayProps: " +<span style="color: #000000"> objectMapper.writeValueAsString(config.getArrayProps()));</br>
System.out.println(</span>"listProp1: " +<span style="color: #000000"> objectMapper.writeValueAsString(config.getListProp1()));</br>
System.out.println(</span>"listProp2: " +<span style="color: #000000"> objectMapper.writeValueAsString(config.getListProp2()));</br>
System.out.println(</span>"mapProps: " +<span style="color: #000000"> objectMapper.writeValueAsString(config.getMapProps()));</br></br></br>



//测试加载properties文件

System.out.println(env.getProperty("name"));//孙凯

System.out.println(env.getProperty("abc"));//null

System.out.println(name);//26

    <span style="color: #0000ff">return</span> "Hello World!"<span style="color: #000000">;</br>
}</span></pre>

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. P1145 约瑟夫

    P1145 约瑟夫 题目描述 n个人站成一圈,从某个人开始数数,每次数到m的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人,k个好人站在一起,k个坏人站在一起.从第一个好人开始数 ...

  2. Qt程序调试之Q_ASSERT断言(它是一个宏,接受布尔值,当其中的布尔值为真时,便什么也不做)

    在使用Qt开发大型软件时,难免要调试程序,以确保程序内的运算结果符合我们的预期.在不符合预期结果时,就直接将程序断下,以便我们修改. 这就用到了Qt中的调试断言 - Q_ASSERT. 用一个小例子来 ...

  3. git 工具的使用总结(6)-提交合并处理

    1.撤消修改 1)revert:反转提交,它就是把你的一个提交先撤消掉,但是,它跟reset不同的是,你的这次这小会留下记录,这样在你下次需要的时候,可以通过这个节点把撤消的提交恢复 zhangshu ...

  4. Android学习笔记进阶20之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

  5. python运算符优先级表

    运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in,not in 成员测试 is,is not 同一性测试 <,<=,> ...

  6. Docker 内程序时间设置,很重要

    原文:Docker 内程序时间设置,很重要 重要!!!!! 创建容器时候需要修改一个参数,设置tomcat的时区 -e TZ="Asia/Shanghai" -v /etc/loc ...

  7. Boost解析xml——xml写入

    <?xml version="1.0" encoding="utf-8"?> <Config> <Item name=" ...

  8. Writing buffer overflow exploits - a tutorial for beginners

    Buffer overflows in user input dependent buffers have become one of the biggest security hazards on ...

  9. activity-栈相关属性

    1.启动任务栈 第一种,动作设置为“android.intent.action.MAIN”,类别设置为“android.intent.category.LAUNCHER”,可以使这个ACT(activ ...

  10. 利用ServiceWorker实现页面的快速加载和离线访问

    Service workers 本质上充当Web应用程序与浏览器之间的代理服务器,也可以在网络可用时作为浏览器和网络间的代理.它们旨在(除其他之外)使得能够创建有效的离线体验,拦截网络请求并基于网络是 ...