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.在项目下创建配置文件 { & ...
随机推荐
- 输出图中顶点i到顶点j之间的所有简单路径
简单路径(不包括环) DFS遍历以及回溯得到结果 void dfs(ALGraph graph, int v, int end, bool visit[], int path[], int cnt) ...
- JS 对象 合并
来自:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Sy ...
- 恢复xfs文件系统superblock实验
1. 创建一个XFS文件系统[root@localhost ~]# mkfs.xfs -f /dev/vdb1meta-data=/dev/vdb1 isize=256 ...
- SecureCRT连接Ubuntu,centos失败,长时间的重新连接,连接不了解决办法
Ubuntu,centos默认未安装ssh远程加密连接服务.使用命令,安装即可. 0.sudo apt-get install openssh-server openssh-client 1.rpm ...
- java 0 开始
利用了61天的时间学习了 se 不过忘得也很多 .在这里开一个帖子 打算利用几天的时间进行复习,把凡是能用到的都放在这边. 不带图形界面的第一个项目已经弄完 (看视频加看书..而且自己往上面加东 ...
- Scrapy框架--cookie的获取/传递/本地保存
环境:Python3.6 + Scrapy1.4 我要实现的东西:1. 完成模拟登陆 2. 登陆成功后提取出cookie,然后保存到本地cookie.txt文件中 3. ...
- Centos 7 安装 Visual stdio Code
最近微软正式发布了.net code 和asp.net code.尝试了下在linux下.net code和asp.net code使用. 具体怎么使用.net code 和asp.net code ...
- Java之IO(十一)BufferedReader和BufferedWriter
转载请注明源出处:http://www.cnblogs.com/lighten/p/7074488.html 1.前言 按照字节流的顺序一样,字符流也提供了缓冲字符流,与字节流不同,Java虽然提供了 ...
- 【数组】Jump Game
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- rails 5.2 启动警告 warning: previous definition of VERSION was here和bootsnap
bootsnap依赖问题 You should add gem 'bootsnap' to your gemfile to install it or remove the line require ...