Spring Boot快速入门之(九):应用属性

【注】本文译自: https://www.tutorialspoint.com/spring_boot/spring_boot_application_properties.htm

Spring Boot快速入门之(九):应用属性

    应用属性能够支持在不同环境中工作。本文将带你学习如何在 Spring Boot 应用中配置特定的属性。

命令行属性

    Spring Boot 应用将命令行属性转换为 Spring Boot 环境属性。命令行属性优先于其他属性源。Spring Boot 默认使用 8080 端口号启动 Tomcat。让我们学习如何使用命令行属性改变它。

第1步: 创建可执行 JAR 文件,使用命令 java –jar <JARFILE> 运行。

第2步: 如下面截屏的命令所示,利用命令行属性改变 Spring Boot 应用的端口号:

Spring Boot快速入门之(九):应用属性

注意: 你可以使用分隔符号 - 提供多个应用属性。

属性文件

    属性文件的作用在于,在不同环境中运行的应用使用单个属性文件配置多个属性。Spring Boot 中属性被配置在 application.properties 文件中,这个文件要在 classpath 路径中。

   application.properties 文件位于 src/main/resources 目录。以下代码是 application.properties 文件的示例:

server.port = 9090

spring.application.name = demoservice

   要注意上面的代码指定 Spring Boot 应用 demoservice 启动的端口号为 9090。

YAML 文件

   Spring Boot 支持基于 YAML 的属性配置来运行应用。代之以 application.properties,我们可以使用 application.yml 文件。这个 YAML 文件也应当在 classpath 路径下。application.yml 文件示例如下:

spring:

   application:

      name: demoservice

   server:

port: 9090

外化属性

   不用在 classpath 路径下的属性文件,我们也可以在不同的位置和路径下使用。在运行 JAR 文件时,也可以指定属性文件路径,如下所示:

-Dspring.config.location = C:\application.properties

Spring Boot快速入门之(九):应用属性

使用 @Value 注解

   @Value 注解用于在 Java 代码中读取环境或应用属性。读取属性的语法如下所示:

@Value("${property_key_name}")

   下面的例子展示了如何在 Java 变量中使用 @Value 注解读取 spring.application.name 的属性值。

@Value("${spring.application.name}")

   看下面的代码,更好理解:

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;

   }

}

注意:如果在运行应用中没有找到指定的属性,Spring Boot 会抛出非法参数异常,如:Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"。

   要解决占位符问题,可以设置属性的缺省值,如下所示:

@Value("${property_key_name:default_value}")

 

@Value("${spring.application.name:demoservice}")

Spring Boot 活动配置

   Spring Boot 支持基于 Spring 活动配置的不同属性。例如,我们可以为开发和生产环境下运行 Spring Boot 应用配置不同的属性文件。

在 application.properties 中的 spring 活动配置

   我们来理解一下如何 application.properties 中的 Spring 活动配置。缺省情况下,Spring Boot 应用会使用 application. properties 来运行。如果你想使用基于属性的配置以如下所示来保存每个配置:

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 活动配置。缺省情况下,Spring Boot 应用使用 application.properties 文件。设置 spring active 配置的命令如下所示:

Spring Boot快速入门之(九):应用属性

   你可以在控制台日志中看到活动配置名,如下所示:

2017-11-26 08:13:16.322 INFO 14028 --- [

main] com.tutorialspoint.demo.DemoApplication :

The following profiles are active: dev

    现在 Tomcat 已经在 9090 (http) 端口启动了,如下所示:

2017-11-26 08:13:20.185 INFO 14028 --- [

main] s.b.c.e.t.TomcatEmbeddedServletContainer :

Tomcat started on port(s): 9090 (http)

    如下所示可以指定生产环境的活动配置:

Spring Boot快速入门之(九):应用属性

   在控制台日志中可以看到活动配置名,如下所示:

2017-11-26 08:13:16.322 INFO 14028 --- [

main] com.tutorialspoint.demo.DemoApplication :

The following profiles are active: prod

   现在 Tomcat 已经在 4431 (http) 端口启动了,如下所示:

2017-11-26 08:13:20.185 INFO 14028 --- [

main] s.b.c.e.t.TomcatEmbeddedServletContainer :

Tomcat started on port(s): 4431 (http)

针对 application.yml 的 Spring 活动配置 

   接下来,我们看下针对 application.yml 如何设置 Spring 活动配置。我们可以在单个 application.yml 文件中设置 Spring 活动配置。不需要象 application.properties 那样使用不同的的属性文件。

   下面是一个 application.yml 文件示例。注意分隔符 (---) 用于隔离在 application.yml 文件中的不同配置。

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

   如下所示设置开发环境的活动配置:

Spring Boot快速入门之(九):应用属性

   如下所示,你可以在控制台日志中看到活动的配置:

2017-11-26 08:41:37.202 INFO 14104 --- [

main] com.tutorialspoint.demo.DemoApplication :

The following profiles are active: dev

   现在 Tomcat 已经在 9090 (http) 端口启动了,如下所示:

2017-11-26 08:41:46.650 INFO 14104 --- [

main] s.b.c.e.t.TomcatEmbeddedServletContainer :

Tomcat started on port(s): 9090 (http)

   在控制台日志中可以看到活动配置名,如下所示:

Spring Boot快速入门之(九):应用属性

   如下所示可以指定生产环境的活动配置:

2017-11-26 08:43:10.743 INFO 13400 --- [

main] com.tutorialspoint.demo.DemoApplication :

The following profiles are active: prod

   现在 Tomcat 已经在 4431 (http) 端口启动了,如下所示:

2017-11-26 08:43:14.473 INFO 13400 --- [

main] s.b.c.e.t.TomcatEmbeddedServletContainer :

Tomcat started on port(s): 4431 (http)