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. .NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)

    尽管构造函数参数注入是传递参数值给当前构造的组件的优先方式,但是你也可以使用属性或者方法注入来提供参数值. 属性注入使用可写入的变量而不是构造函数参数来完成注入.方法注入则通过方法来设置依赖项. 属性 ...

  2. HTTPS 怎样保证数据传输的安全性

    大家都知道,在客户端与服务器数据传输的过程中,HTTP协议的传输是不安全的,也就是一般情况下HTTP是明文传输的.但HTTPS协议的数据传输是安全的,也就是说HTTPS数据的传输是经过加密的. 在客户 ...

  3. 当强制关机时,出现Eclipse打不开的问题

    关于Eclipse或MyEclipse启动卡死的问题(即Eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法):      方案1:(推荐使用,如果没有这个文件,就使用方案2,方案2基本上 ...

  4. linux日常运维常用命令

    ---查看端口占用 netstat -ap | grep 8000 ---重启nginx sudo /usr/sbin/nginx -c /usr/local/nginx/conf/nginx.con ...

  5. 关于小窗滑动,父级body也跟随滑动的解决方案

    需求:当前页面是信息列表,所以高度由内容自动填充, 所以页面可以上下滑动,加载更多, 但是下发物料一栏又为一个列表 所以做了一个弹窗框,因为是列表所以高度自然又是不可控的,所以给了一个最大高度,当超出 ...

  6. .NET内存管理、垃圾回收

    1. Stack和Heap    每个线程对应一个stack,线程创建的时候CLR为其创建这个stack,stack主要作用是记录函数的执行情况.值类型变量(函数的参数.局部变量 等非成员变量)都分配 ...

  7. 从零开始学 Web 之 JavaScript(三)函数

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

  8. 自己动手,写一个分布式系统(附c#代码示例)

    分布式系统有很多成熟的解决方案.如:微软的WCF.WCF太过于复杂,配置也麻烦.其实可以自己动手设计一个小的分布式系统.系统的原理完全在自己掌握之中,可以根据业务随机而变.这里展示远程调用最核心最基本 ...

  9. ElasticSearch实战-编码实践

    1.概述 前面在<ElasticSearch实战-入门>中给大家分享如何搭建这样一个集群,在完成集群的搭建后,今天给大家分享如何实现对应的业务功能模块,下面是今天的分享内容,目录如下所示: ...

  10. SpringBoot+Mybatis+Pagehelper分页

    1.pom.xml <!-- mybatis分页插件 --> <dependency> <groupId>com.github.pagehelper</gro ...