springboot读取自定义的properties文件:

    

package com.huhy.demo.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; /**
* @author : huhy on 2018/9/28.
* @Project_name:springboot_self_gitlab
* @LOCAL:com.huhy.demo.properties
* @description:
* 注意:
* 1》spring-boot更新到1.5.2版本后locations属性无法使用
* @PropertySource注解只可以加载proprties文件,无法加载yaml文件
* 2》 如果有多个配置文件需要注入,可以用value[]来接收
@PropertySource(value={"classpath:yang.properties","classpath:yang2.properties"})
3》文件读取 默认是resource下文件
@PropertySource("classpath:config/remote.properties") 配置config文件路径
4》加上encoding = "utf-8"属性防止中文乱码,也可以大写的"UTF-8"
5》ignoreResourceNotFound = true 扫描文件不存在的处理方式 默认false
*/
@ConfigurationProperties(prefix="huhy")
@PropertySource(value = {"classpath:yang.properties","classpath:yang2.properties"},encoding = "UTF-8",ignoreResourceNotFound = true)
@Data
@Component
public class PropertiesYang { private String name;
private String age; /**
*
* name的值我们设置的是"classpath:yang.properties","classpath:yang2.properties"。这个值在Springboot的环境中必须是唯一的,如果不设置,
* 则值为:“class path resource ["classpath:yang.properties","classpath:yang2.properties"]“。
* 可能很多人比较纳闷,为什么是“class path resource ["classpath:yang.properties","classpath:yang2.properties"]“呢?
* 这个就涉及到了Spring中对资源文件的封装类Resource。上文我们配置的value值为""classpath:yang.properties","classpath:yang2.properties"",
* 因此Spring发现是classpath开头的,因此最终使用的是Resource的子类ClassPathResource。如果是file开头的,则最终使用的类是FileSystemResource。
* 了解了上文所述的Resource类之后。我们再次明确一点,如果@PropertySource中如果没有设置name值,则name值的生成规则是:根据value值查找到最终封装的Resource子类,
* 然后调用具体的Resource子类实例对象中的getDescription方法,getDescription方法的返回值为最终的name值。
* 比如ClassPathResource类中的getDescription方法实现如下:
* public String getDescription() {
* StringBuilder builder = new StringBuilder("class path resource [");
* String pathToUse = path;
* if (this.clazz != null && !pathToUse.startsWith("/")) {
* builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
* builder.append('/');
* }
* if (pathToUse.startsWith("/")) {
* pathToUse = pathToUse.substring(1);
* }
* builder.append(pathToUse);
* builder.append(']');
* return builder.toString();} * */
}

springboot 读取自定义的yml文件:

  由于springboot1.5.2之后停止了localtions的指定。现在加载yml文件的实现方式如下:

    YmlConfig  配置类:

    

/**
* @author : huhy on 2018/9/28.
* @Project_name:springboot_self_gitlab
* @LOCAL:com.huhy.demo.properties
* @description:{todo}
*/
@Component
public class YmlConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//yaml.setResources(new FileSystemResource("yang.yml"));//File引入
yaml.setResources(new ClassPathResource("yang.yml"));//class引入
configurer.setProperties(yaml.getObject());
return configurer;
}
}

实体类:

  

/**
* @author : huhy on 2018/9/28.
* @Project_name:springboot_self_gitlab
* @LOCAL:com.huhy.demo.properties
* @description:自定义加载yml文件
* 1> ConfigurationProperties注解的locations属性在1.5.X以后没有了,不能指定locations来加载yml文件
* PropertySource注解只支持properties文件加载,详细见官方文档: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings
* 2>
*/
@Data
@Component
@ConfigurationProperties(prefix = "yang")
public class YmlYang{ private String name;
private String age;
}

配置文件“

  

yang:
name: yml
age:

就这样就行了,大家可以试一下

springboot 学习之路 22 (读取自定义文件)的更多相关文章

  1. Springboot学习06-Spring AOP封装接口自定义校验

    Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...

  2. springboot 学习之路 1(简单入门)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  3. springboot 学习之路 3( 集成mybatis )

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  4. springboot 学习之路 4(日志输出)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  5. springboot 学习之路 6(定时任务)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  6. springboot 学习之路 8 (整合websocket(1))

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  7. springboot 学习之路 5(打成war包部署tomcat)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  8. springboot 学习之路 7(静态页面自动生效问题)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  9. springboot 学习之路 9 (项目启动后就执行特定方法)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

随机推荐

  1. Liferay平台开发使用详细PPT演示文稿

    主要章节: 概述 功能和使用 开发扩展 安全.认证 高可用 Demo 独立流程演示工程: Liferay集成Activiti开发工程: PPT演示文稿下载 Demo程序分2部分: 独立流程演示工程:h ...

  2. Liferay7 BPM门户开发之22: Liferay7模型监听器(Model Listeners)

    Model Listeners实现ModelListener接口,用于持久化时的AOP处理 一些使用场景: Audit Listener: 在一个独立而分离的数据库,做信息更新的审计: Cache C ...

  3. Liferay7 BPM门户开发之11: Activiti工作流程开发的一些统一规则和实现原理(完整版)

    注意:以下规则是我为了规范流程的处理过程,不是Activiti公司的官方规定. 1.流程启动需要设置启动者,在Demo程序中,“启动者变量”名统一设置为initUserId 启动时要做的: ident ...

  4. 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类

    BigInteger类 发 package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以让超过Integer范围内的数据进 ...

  5. 关于jquery中prev()和next()的用法

    用prev()和next()方法动态的添加class.以达到当前元素的前面几个元素或后面的几个元素添加class <body> <ul> <li>1</li& ...

  6. EasyNetQ中使用自定义的ISerializer

    最近在使用EasyNetQ时,遇到一个问题:c++项目组发送的消息数据不是Json数据,而是自定义的数据格式(各字段+‘|’连接成一个字符串),EasyNetQ中消费消息接收的都是强类型,没办法直接消 ...

  7. springmvc的异步处理

         关于异步的好处我在这里就不多说了,自从servlet3.1规范发布以来,控制层的异步处理也越来越多的被人提及.而Spring5的webflux诞生也意味着Spring全方位对异步提供了支持. ...

  8. 从零开始学 Web 之 DOM(三)innerText与innerHTML、自定义属性

    大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...

  9. Centos7安装Mysql8(官方整合包)

    1. 下载整合包 [root@master ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-1.el7.x86_ ...

  10. 对python pickle的理解

    python 提供了pickle模块,能将对象进行序列化,将对象以文件形式存放在磁盘. 几乎所有的数据类型(列表,字典,集合,类等)都可以用pickle来序列化.但是序列化后的数据可读性很差. pic ...