Spring Boot应用程序属性,应用程序属性支持我们在不同的环境中工作。在本章中,您将学习如何配置和指定 Spring Boot 应用程序的属性。

命令行属性

Spring Boot 应用程序将命令行属性转换为 Spring Boot 环境属性。命令行属性优先于其他属性源。默认情况下,Spring Boot 使用 8080 端口号启动 Tomcat。让我们了解如何使用命令行属性更改端口号。

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

第 2 步– 使用下面给出的屏幕截图中给出的命令,通过命令行属性更改 Spring Boot 应用程序的端口号。

command_line_properties_jarfile

注意– 您可以使用定界符 – 提供多个应用程序属性。

Properties 文件

属性文件用于在单个文件中保留“N”个属性,以便在不同的环境中运行应用程序。在 Spring Boot 中,属性保存在类路径下的application.properties文件中。

application.properties 文件位于src/main/resources目录中。示例application.properties文件的代码如下 –

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

请注意,在上面显示的代码中,Spring Boot 应用程序演示服务在端口 9090 上启动。

YAML 文件

Spring Boot 支持基于 YAML 的属性配置来运行应用程序。我们可以使用application.yml文件代替application.properties。这个 YAML 文件也应该保存在类路径中。示例application.yml文件如下 –

spring:
   application:
      name: demoservice
   server:
port: 9090

外化属性

我们可以将属性保存在不同的位置或路径中,而不是将属性文件保存在类路径下。在运行 JAR 文件时,我们可以指定属性文件路径。您可以在运行 JAR 时使用以下命令指定属性文件的位置 –

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

externalized_properties

@Value 注解的使用

@Value 注释用于读取 Java 代码中的环境或应用程序属性值。读取属性值的语法如下所示 –

@Value("${property_key_name}")

查看以下示例,它显示了使用 @Value 注释读取Java 变量中的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}”

要解决占位符问题,我们可以使用下面给出的 thr 语法设置属性的默认值 –

@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

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 文件。设置弹簧活动配置文件的命令如下所示 –

prod_properties_active_dev

您可以在控制台日志中看到活动的配置文件名称,如下所示 –

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)

您可以设置生产活动配置文件,如下所示 –

production_active_profile

您可以在控制台日志中看到活动的配置文件名称,如下所示 –

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 活动配置文件。我们可以将 Spring 活动配置文件属性保存在单个application.yml文件中。无需使用像 application.properties 这样的单独文件。

以下是将 Spring 活动配置文件保存在 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

下面给出了设置开发活动配置文件的命令 –

prod_properties_active_dev (1)

您可以在控制台日志中看到活动的配置文件名称,如下所示 –

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)

设置生产活动配置文件的命令如下 –

production_active_profile

您可以在控制台日志中看到活动的配置文件名称,如下所示 –

2017-11-26 08:43:10.743  INFO 13400 --- [    
   main] com.tutorialspoint.demo.DemoApplication  : 
   The following profiles are active: prod

这将在端口 4431 (http) 上启动 Tomcat,如下所示:

2017-11-26 08:43:14.473  INFO 13400 --- [     
   main] s.b.c.e.t.TomcatEmbeddedServletContainer : 
   Tomcat started on port(s): 4431 (http)

Spring Boot应用程序属性 推荐阅读

利用GPT-4进行调试和错误修复

如何使用Git Merge

领券有优惠