关于springboot:Spring-Boot快速入门之九应用属性

5次阅读

共计 4052 个字符,预计需要花费 11 分钟才能阅读完成。

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

利用属性可能反对在不同环境中工作。本文将带你学习如何在 Spring Boot 利用中配置特定的属性。

命令行属性

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

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

第 2 步:如上面截屏的命令所示,利用命令行属性扭转 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

应用 @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 配置的命令如下所示:

你能够在控制台日志中看到流动配置名,如下所示:

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)

如下所示能够指定生产环境的流动配置:

在控制台日志中能够看到流动配置名,如下所示:

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

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

如下所示,你能够在控制台日志中看到流动的配置:

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)

在控制台日志中能够看到流动配置名,如下所示:

如下所示能够指定生产环境的流动配置:

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)

正文完
 0