SpringBoot(三) Core Features: External Configuration(配置文件)
可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置
一、属性值可以直接注入到bean
系统属性值不可以
// application.properties
name=xiaoming
server.port=80
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
@Component
public class MyBean {
@Value("${name}")
private String name;
@Value("${server.port}") // 不可以
private String name;
// ...
}
二、属性作为参数应用
文档: 24.5 Placeholders in Properties
server.port=80
# 自定义属性
xm.name=xiaoming
xm.gener=man
xm.reference=Name:${xm.name},Gener:${xm.gener},${server.port} // 系统属性貌似不可以
三、配置随机数(Configuring Random Values)
四、通过命令行设置属性值(Accessing Command Line Properties)
可以禁用
SpringApplication.setAddCommandLineProperties(false)
五、多环境配置
通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包
六、关于自定义配置文件
- 自定义配置
peron:
lastName: xiaoming
last: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 2
- 使用注解映射属性的值
@Component
@ConfigurationProperties("person")
public class Person {
private String lastName;
private Integer age;
...
}
- 声明依赖 (见附录B)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 主程序类启用
@EnableConfigurationProperties注解
@SpringBootApplication
@EnableConfigurationProperties
public class HelloWorldMainApplication {
...
}
参考
- 文档: 24.3 Application Property Files
- 附录B.配置元数据: B.3 Generating Your Own Metadata by Using the Annotation Processor
七、YAML
不可以使用 @PropertySource注解来加载, 只能加载properties文件
八、参考
SpringBoot(三) Core Features: External Configuration(配置文件)的更多相关文章
- SpringBoot(四) Core Features: Logging
参考 文档: 26. Logging
- SpringBoot(二) Core Features: SpringApplication
参考 文档: SpringApplication
- springboot三种配置文件上传下载大小的配置
配置文件为application.yml格式: spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: ...
- Java开发学习(三十六)----SpringBoot三种配置文件解析
一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...
- External Configuration Store Pattern 外部配置存储模式
Move configuration information out of the application deployment package to a centralized location. ...
- [.NET Core] 简单读取 json 配置文件
简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...
- ASP.NET Core 1.0 Configuration 配置管理
documentation: https://docs.asp.net/en/latest/fundamentals/configuration.html github: https://github ...
- Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...
- 干货:.net core实现读取自定义配置文件,有源代码哦
看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息.这一篇教大家自定义配置文件: 1.在项目下创建配置文件 { & ...
随机推荐
- openstack 实用命令
port 1.创建port(create) i.随机ip openstack port create --network public --fixed-ip subnet=sub-public '' ...
- 守护进程与Supervisor
博客链接:http://www.cnblogs.com/zhenghongxin/p/8676565.html 消息队列处理后台任务带来的问题 在系统稍微大些的时候,我们经常会用到消息队列(实现的方式 ...
- 792. Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- 698. Partition to K Equal Sum Subsets
Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...
- docker registry 私有仓库 安装配置、查询、删除
#++++++++++++++++++++++++++++++ #docker-registry 私有仓库 #搜索,下载register镜像 docker search registry docker ...
- jzoj5894
先前綴和一發,問題表示求[0-l2][0-r2]滿足條件的數的個數 假設可以把某一個數拆分成[前面任意個數][00-0-11-1(個數相同)]的區間 那麼問題會簡單的多,因為任意一個a位的整數分別xo ...
- 前端入门html(常用标签及标签分类)
day47 参考:https://www.cnblogs.com/liwenzhou/p/7988087.html 任何标签都有有三个属性:ID,class.style <!DOCTYPE ht ...
- zoj4016 Mergeable Stack
题意:对n个栈,有q次操作.每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端:2.输出s栈的栈顶(若栈为空则输出empty):3.将栈t插入到栈s的栈顶. 开始考虑到指针可能会mle,用数组模 ...
- Java并发工具类之线程间数据交换工具Exchanger
Exchanger是一个用于线程间协做的工具类,主要用于线程间的数据交换.它提供了一个同步点,在这个同步点,两个线程可以彼此交换数据.两个线程通过exchange方法交换数据,如果一个线程执行exch ...
- Python 3 虚拟机端口映射 VMware
编程语言(以Python 3 为例子) 编程语言最开始就是机器语言(低级语言,计算机能认识:0101... 最底层的指令代码),学习很困难.现在用机器语言的很少,芯片厂商的技术人员来编程之外:发展下来 ...