读取application文件

在application.yml或者properties文件中添加:

info.address=USA
info.company=Spring
info.degree=high

@Value注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class InfoConfig1 { @Value("${info.address}")
private String address; @Value("${info.company}")
private String company; @Value("${info.degree}")
private String degree; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String getDegree() {
return degree;
} public void setDegree(String degree) {
this.degree = degree;
} }

@ConfigurationProperties注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Component
@ConfigurationProperties(prefix = "info")
public class InfoConfig2 { private String address;
private String company;
private String degree; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String getDegree() {
return degree;
} public void setDegree(String degree) {
this.degree = degree;
} }

读取指定文件

资源目录下建立config/db-config.properties:

db.username=root\
db.password=123456

@PropertySource+@Value注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Component
@PropertySource(value = { "config/db-config.properties" })
public class DBConfig1 { @Value("${db.username}")
private String username; @Value("${db.password}")
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

注意:@PropertySource不支持yml文件读取。

@PropertySource+@ConfigurationProperties注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = { "config/db-config.properties" })
public class DBConfig2 { private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

Environment读取方式

以上所有加载出来的配置都可以通过Environment注入获取到。

1
2
3
4
5
@Autowired
private Environment env; // 获取参数
String getProperty(String key);

总结

从以上示例来看,Spring Boot可以通过@PropertySource,@Value,@Environment,@ConfigurationProperties来绑定变量。

http://www.javastack.cn/article/2017/spring-boot-load-config/

Spring Boot读取配置的几种方式的更多相关文章

  1. Spring Boot读取配置的 5 种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company=Spring info.degree= ...

  2. Spring Boot读取配置文件的几种方式

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...

  3. Spring Boot 整合 Shiro ,两种方式全总结!

    在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...

  4. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  5. Spring Boot集成MyBatis的2种方式

    目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...

  6. Spring Boot应用启动的三种方式

    Spring Boot应用HelloWorld的三种启动方式: 项目的创建可以在http://start.spring.io/网站中进行项目的创建. 首先项目结构: 1.  通过main方法的形式启动 ...

  7. Spring Boot 实现定时任务的 4 种方式

    作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...

  8. 【websocket】spring boot 集成 websocket 的四种方式

    集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...

  9. Spring Boot — 运行应用程序5种方式

    1. 从IDE中的Run 按钮运行 你可以从IDE中运行Spring Boot应用, 就像一个简单的Java应用, 但是, 你首先需要导入项目. 导入步骤跟你的IDE和构建系统有关. 大多数IDEs能 ...

随机推荐

  1. Docker命令详解(run篇)

    命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Usage: Run a command in a new container 中文意思为:通过r ...

  2. [dev] 啥是Virtual Private Network

    先来读wiki:https://en.wikipedia.org/wiki/Virtual_private_network 摘要: VPNs can be either remote-access ( ...

  3. 20144306《网络对抗》MAL_恶意代码分析

    一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控? 使用Windows自带的schtasks指 ...

  4. 17.4-uC/OS-III消息管理(任务消息队列使用)

    任务消息队列跟任务信号量一样,均隶属于某一个特定任务, 不需单独创建,任务在则在, 只有该任务才可以接收这个任务消息队列的消息,其他任务只能给这个任务消息队列发送消息, 却不能接收.任务消息队列与(普 ...

  5. 20190404 Oracle忘记登陆密码

    记忆力不好,总是忘记Oracle账号的登陆密码 修改方式 Windows cmd 登陆修改后的密码即可

  6. Apache环境下配置多个站点的SSL证书

    重新创建apache目录中conf/extra/下的httpd-ssl.conf文件 NameVirtualHost *:443 Listen 443 <VirtualHost *:443> ...

  7. python second lesson

    1.系统模块 新建的文件名不能和导入的库名相同,要不然python会优先从自己的目录下寻找. import sys  sys是一个系统变量,sys.argv会调出文件的相对路径,sys.argv[2] ...

  8. fastjson java类、字符串、jsonObject之前的转换

    json对象转成json字符串 JSONObject json = new JSONObject(); json.put("page",1); json.put("pag ...

  9. 使用git将项目上传到github(最简单方法)

    首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: https://gi ...

  10. arithmetic-02

    Java collection API 中实现的表ADT: collection<E>接口实现继承iterable<E>接口,实现iterable接口的类可以使用增强for循环 ...