Spring Boot2.0自定义配置文件使用
声明:
- spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用
- 根据Spring Boot2.0官方文档,PropertySource注解,只支持properties文件,因此排除 YAML配置
- 针对二,可考虑新建配置类,自行搜索,不再此次讨论范围
项目路径下具体使用:
1.根目录下新建自定义配置文件夹与properties配置文件
example.name=tom
example.wife=jerry
example.age=25
example.dream=top
2.创建配置文件对应的实体类
@ConfigurationProperties(prefix = "example")
@PropertySource(value = "classpath:config/config-test.properties")
@Component
public class ExampleConfig {
private String name;
private String wife;
private String age;
private String dream; get ... set
}
3.注入,直接调用
// 自定义配置文件对应配置类 注入
@Autowired
private ExampleConfig exampleConfig;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String sayHello(){
return exampleConfig.getName() + "的老婆是"
+ exampleConfig.getWife() + ", 年龄" + exampleConfig.getAge()
+ "岁,梦想是" + exampleConfig.getDream();
}
文件目录下具体使用
生产环境中,小型项目可能需要指定具体目录下的自定义配置文件,避免反复的打包部署,实操如下:
1.yaml配置自定义配置文件在服务器的路径
config:
location: /data/rosetta/config/textclf-config.properties
2.创建配置文件对应的实体类
@ConfigurationProperties(prefix = "example")
@PropertySource(value = "file:${config.location}")
@Component
public class WeightScope { private String name;
private String scope; set ... get
}
注意:区别于项目路径下的 classpath ,服务器具体节点的配置使用 file
3.具体调度
// 注入自定义配置类
@Autowired
private WeightScope weightScope; // 直接使用即可
logger.info("request--weightScope:"+weightScope.getScope());
知识储备:
ConfigurationProperties源码:
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
/**
* The name prefix of the properties that are valid to bind to this object. Synonym
* for {@link #prefix()}.
* @return the name prefix of the properties to bind
*/
@AliasFor("prefix")
String value() default "";
/**
* The name prefix of the properties that are valid to bind to this object. Synonym
* for {@link #value()}.
* @return the name prefix of the properties to bind
*/
@AliasFor("value")
String prefix() default "";
boolean ignoreInvalidFields() default false;
boolean ignoreUnknownFields() default true;
}
Spring Boot2.0自定义配置文件使用的更多相关文章
- IntellJ IDEA 对于 Spring Boot2.0.2 配置文件application.properties 配置
1.指定文件名称: 打开IDEA编辑位置: 找到如图位置: 点开后方设置 设置输入如下内容:(默认的配置文件名字可以使用--spring.config.name来指定,只需要指定文件的名字,文件扩展名 ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
- (C)spring boot读取自定义配置文件时乱码解决办法
这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...
- Spring Boot2.0之 监控管理
Spring boot监控中心: 针对微服务的服务状态,服务器的内存变化(内存.线程.日志管理等)检测服务配置连接地址是否有用(有些懒加载的情况下,用的时候发现卧槽不能用)模拟访问,懒加载.统计有多少 ...
- Spring Boot2.0使用Spring Security
一.Spring Secutity简介 Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...
- spring boot2.0(一 ) 基础环境搭建
1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...
- Spring Boot2.0 整合 Kafka
Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...
- spring boot 2.0(一)权威发布spring boot2.0
Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...
- 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】
环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...
随机推荐
- ASP.NET Core 发布之后通过命令控制监听地址和环境变量
添加Command支持 新建一个ASP.NET Core 项目,打开Program.cs 添加下面的代码: public class Program { public static void Main ...
- 【C# 复习总结】类、继承和接口
1 类 定义新的数据类型以及这些新的数据类型进行相互操作的方法 定义方式: class Cat { } class Cat:object { } C#中所有的类都是默认由object类派生来的,显示指 ...
- 朱晔和你聊Spring系列S1E4:灵活但不算好用的Spring MVC
阅读PDF版本 本文会以一些例子来展现Spring MVC的常见功能和一些扩展点,然后我们来讨论一下Spring MVC好用不好用. 使用SpringBoot快速开始 基于之前的parent模块,我们 ...
- oracle树形结构层级查询之start with ....connect by prior、level、order by以及sys_connect_by_path之浅谈
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- Django的路由层
U RL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代 ...
- 关于php,python,javascript文件或者模块导入引入的区别和联系
前言: 我们经常看到编程语言之间,文件或者模块的引来引去的,但是他们在各个编程语言之间有什么区别和联系呢? 1.javascript (1).全局引入方式: <script src='xxxxx ...
- 在k-means或kNN,我们是用欧氏距离来计算最近的邻居之间的距离。为什么不用曼哈顿距离?
曼哈顿距离只计算水平或垂直距离,有维度的限制.另一方面,欧氏距离可用于任何空间的距离计算问题. 因为,数据点可以存在于任何空间,欧氏距离是更可行的选择.例如:想象一下国际象棋棋盘,象或车所 做的移动是 ...
- 软件工程(FZU2015) 赛季得分榜,第10回合(alpha冲刺)
SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...
- AQS解析(未完成)
参考:Java并发之AQS详解 同步队列和condition等待队列.获取到锁的线程则处于可运行状态,而未获取到锁的线程则被添加到同步队列中,等待获取到锁的线程释放锁. 一.数据结构 Node sta ...
- Linux之磁盘挂载
1.查看磁盘分区情况: fdisk -l 可以看到,红框中的硬盘没有分区. 2.开始分区: fdisk /dev/vdb 3.格式化分区: mkfs.xfs 分区名 4.挂载磁盘 挂载方式1: 手动挂 ...