springboot 学习之路 22 (读取自定义文件)
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 (读取自定义文件)的更多相关文章
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- springboot 学习之路 1(简单入门)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 3( 集成mybatis )
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 4(日志输出)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 6(定时任务)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 8 (整合websocket(1))
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 5(打成war包部署tomcat)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 7(静态页面自动生效问题)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springboot 学习之路 9 (项目启动后就执行特定方法)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
随机推荐
- linux下的shell脚本的使用
什么是shell? Shell是一个命令解释器,它在操作系统的最外层,负责直接与用户进行对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出到屏幕反馈给用户.这种对话方式可是交互 ...
- 机器学习与Tensorflow(7)——tf.train.Saver()、inception-v3的应用
1. tf.train.Saver() tf.train.Saver()是一个类,提供了变量.模型(也称图Graph)的保存和恢复模型方法. TensorFlow是通过构造Graph的方式进行深度学习 ...
- jenkins内部分享ppt
持续集成Continuous integration简介(持续集成是什么) .持续集成源于极限编程(XP),是一种软件实践,软件开发过程中集成步骤是一个漫长并且无法预测的过程.集成过程中可能会爆 ...
- Hibernate框架 主配置文件(Hibernate.cfg.xml)基本
数据库连接参数配置: <?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type ...
- Netty心跳机制
一.概念介绍网络中的接收和发送数据都是使用操作系统中的SOCKET进行实现.但是如果此套接字已经断开,那发送数据和接收数据的时候就一定会有问题.可是如何判断这个套接字是否还可以使用呢?这个就需要在系统 ...
- 使用commit方式构建具有sshd服务的centos镜像
一般我们是通过SSH服务来管理服务器的,但是现在很多Docker镜像不带SSH服务,那我们该如何来管理这些容器呢?现在我们通常使用attach和nsenter工具.但是都无法解决远程管理容器的问题,当 ...
- mysql计算两个日期相差的天数
DATEDIFF() 函数可以返回两个日期之间的天数. 如下: SELECT DATEDIFF('2015-06-29','2015-06-12') AS DiffDate 结果得17 SELECT ...
- SQL优化原则(转)
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统 ...
- Python机器学习笔记:不得不了解的机器学习面试知识点(1)
机器学习岗位的面试中通常会对一些常见的机器学习算法和思想进行提问,在平时的学习过程中可能对算法的理论,注意点,区别会有一定的认识,但是这些知识可能不系统,在回答的时候未必能在短时间内答出自己的认识,因 ...
- 基于vue框架手写一个notify插件,实现通知功能
简单编写一个vue插件,当点击时触发notify插件,dom中出现相应内容并且在相应时间之后清除,我们可以在根组件中设定通知内容和延迟消失时间. 1. 基础知识 我们首先初始化一个vue项目,删除不需 ...