声明:

  1. spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用
  2. 根据Spring Boot2.0官方文档,PropertySource注解,只支持properties文件,因此排除 YAML配置
  3. 针对二,可考虑新建配置类,自行搜索,不再此次讨论范围

项目路径下具体使用:

  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自定义配置文件使用的更多相关文章

  1. IntellJ IDEA 对于 Spring Boot2.0.2 配置文件application.properties 配置

    1.指定文件名称: 打开IDEA编辑位置: 找到如图位置: 点开后方设置 设置输入如下内容:(默认的配置文件名字可以使用--spring.config.name来指定,只需要指定文件的名字,文件扩展名 ...

  2. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

  3. (C)spring boot读取自定义配置文件时乱码解决办法

    这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...

  4. Spring Boot2.0之 监控管理

    Spring boot监控中心: 针对微服务的服务状态,服务器的内存变化(内存.线程.日志管理等)检测服务配置连接地址是否有用(有些懒加载的情况下,用的时候发现卧槽不能用)模拟访问,懒加载.统计有多少 ...

  5. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

  6. spring boot2.0(一 ) 基础环境搭建

    1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...

  7. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

  8. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  9. 【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- ...

随机推荐

  1. JavaEE学习之JPA中配置文件persistence.xml

    下面是我从网上找到的关于JPA配置文件persistence.xml的相关描述: <?xml version="1.0" encoding="UTF-8" ...

  2. 使用hibernate造成的MySql 8小时问题解决方案

    本文借鉴了网上的很多博客,在此不再声明 总结 1.增加 MySQL 的 wait_timeout 属性的值(不推荐) mysql5之前的版本,可以在jdbc连接的url中加入:autoReconnec ...

  3. Node.js这么下去...

    Node.js是基于javascript的.event驱动的单进程服务器(也能实现cluster模式,只要一个fork()语句,类似于C语言的进程创建). 所以大胆估计:Node.js会把很多大网站吞 ...

  4. Groovy语言学习--语法基础(2)

    集合和闭包 因为之前没接触过C++等,对指针也一窍不通.个人不成熟的了解 闭包是一种数据类型,可以很方便的执行一段独立的代码 简化方法的调用 package groovy /** * Groovy容器 ...

  5. 吉特日化MES-日化生产称料基本步骤

    在日化行业称料是一个非常重要的环节,整个生产过程中称料所占据的时间也比较长,特别是遇到对料体精度高,量大的情况下称料都比较困难,汇总一下人工称料的基本过程: (1) 称量任务准备:根据生产工单或者生产 ...

  6. 几种事务的隔离级别,InnoDB如何实现?

    事务ACID特性,其中I代表隔离性(Isolation). 什么是事务的隔离性? 隔离性是指,多个用户的并发事务访问同一个数据库时,一个用户的事务不应该被其他用户的事务干扰,多个并发事务之间要相互隔离 ...

  7. 网络应用简记(4):DNS使用

    dns,domain name system,域名系统,把域名转化成ip的系统. 先来看几上工具的使用,这几个工具都能把域名转换成ip,都使用了dns.dns就好比数据库,通过对它的查询,能给url找 ...

  8. 朱晔的互联网架构实践心得S1E1:Pilot

    朱晔的互联网架构实践心得S1E1:Pilot 最近几年写博客确实写得少了,初出茅庐的时候什么都愿意去写,现在写一点东西之前会反复斟酌是否有价值.工作十几年了,做了N多个互联网系统,业务涉及教育.游戏. ...

  9. 【fetch跨域请求】cors

    当使用fetch 发起跨域请求时,CORS(跨域资源共享Cross-origin resource sharing) 请求fetch const body = {name:"Good boy ...

  10. H5 22-通配符选择器

    22-通配符选择器 我是标题 我是段落 我是超链接 --> 我是标题 我是段落 我是超链接 <!DOCTYPE html> <html lang="en"& ...