2019-04-21 | spring | UNLOCK

Spring Boot - Application Properties


应用属性文件(Application Properties)能够支持不同的工作环境,在这个注解中,你将学到怎么配置和指定SpringBoot 的属性文件

##Command Line Properties
SpringBoot应用会转换命令行参数配置到应用环境配置,并且命令行配置优于其他属性源。SpringBoot默认使用8080端口来启动tomcat,让我们学习怎么通过命令行参数来改变端口号。

  1. 打包了一个可执行jar包后,使用默认参数指令java -jar 来启动应用
  2. 如下命令来改变端口号:java -jar demo.jar --server.port=9090
    你可以使用分隔符 - 来指定多个属性

Properties File

属性文件用于在单个文件中暴露N个属性,以在不同的环境中运行应用程序。在SpringBoot中,属性保存在类路径文件下的application.properties文件。
application.properties文件在src/main/resources目录下,如下案例:

1
2
server.port = 9090
spring.application.name = demoservice

YAML File

SpringBoot支持基于YAML格式的属性配置文件来启动应用程序,这次我们使用的不是application.yml文件而是application.properties文件,这个YAML文件照样保存在类路径下,案例如下:

1
2
3
4
5
spring:
application:
name: demoservice
server:
port: 9090

Externalized Properties

如果配置文件并不在类路径下,我们可以在执行jar包时指定路径,你可以使用如下命令来指定属性文件的路径:
-Dspring.config.location = C:\application.properties

使用@Value注解

我们可以使用@Value注解来读取环境中或者java应用属性值,读取变量值如下:@Value("${property_key_name}")
我们可以更好的从代码中发现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {
@Value("${spring.application.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping(value = "/")
public String name() {
return name;
}
}

Note : 如果当运行应用程序的时候没有发现对于的变量值,SpringBoot会抛出非法异常类似于Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"
为了解决这个错误,我们可以使用如下语法设置默认变量值,

1
2
@Value("${property_key_name:default_value}")
@Value("${spring.application.name:demoservice}")

Spring Boot Active Profile

SpringBoot支持基于不同的活动配置文件的不同属性,例如,我们可以保持两个文件为开发和生产环境中运行SpringBoot应用程序

Spring active profile in application.properties

让我们了解一下怎么配置不同的活动应用属性文件。SpringBoot应用程序默认使用application.properties文件,如果我们想使用基于配置文件的属性,我们可以为每个配置文件保留单独的属性文件,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
*application.properties*

server.port = 8080
spring.application.name = demoservice
==========================================
*application-dev.properties*

server.port = 9090
spring.application.name = demoservice
==========================================
*application-prod.properties*

server.port = 4431
spring.application.name = demoservice

当运行jar包时,我们需要指定spring活动配置文件,指令如下:
java -jar demo.jar --spring.profiles.active=dev

Spring active profile for application.yml

让我们了解一下如何通过application.yml文件配置文件,我们可以在一个单独的application.properties文件中声明不同的活动配置,不需要像application.properties文件分开独立,案例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
spring:
application:
name: demoservice
server:
port: 8080

---
spring:
profiles: dev
application:
name: demoservice
server:
port: 9090

---
spring:
profiles: prod
application:
name: demoservice
server:
port: 4431
port: 4431

当运行jar包时,我们需要指定spring活动配置文件,指令如下:
java -jar demo.jar --spring.profiles.active=dev

评论加载中