springboot极简教程004-多环境配置

2018/10/28 springboot 共 760 字,约 3 分钟

在resources目录下创建application.yml,application-dev.yml,application-prod.yml配置文件,在application.yml中添加以下配置:

spring:
  profiles:
    active: prod

application-dev.yml:

version: TEST

application-prod.yml:

version: PROD

也即当前启用prod的配置文件,那么配置文件会使用application-prod.yml而不是application-dev.yml,

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
http://localhost:8080/

把application.yml中的active改为dev再试一下。
* */

@RestController
@EnableAutoConfiguration
public class HelloController {
    @Value("${version}")
    private String version;

    @RequestMapping("/")
    private String index() {
        return String.format("Online: %s", version);
    }
}

文档信息

Search

    Table of Contents