1. Spring Boot不同环境不同配置

在不同的环境下激活不同的配置,可以使用Profiles,如下代码中配置了两个Profiles。

spring:
  profiles: mysql
jdbc:
  driver:
    com.mysql.jdbc.Driver
---
spring:
  profiles: oracle
jdbc:
  driver:
    oracle.jdbc.driver.OracleDriver

定义了mysql与oracle两个profiles,profiles间使用“---”进行分割。在Spring容器启动时,使用spring.profiles.active来指定激活那个profiles,如下:

ConfigurableApplicationContext context = new SpringApplicationBuilder(
                TestProfiles.class)
                .properties(
                        "spring.config.location=classpath:/test-profiles.yml")
                .properties("spring.profiles.active=oracle").run(args);
Copyright © 神都花已开 2021 all right reserved,powered by Gitbook修订时间: 2021-11-01 15:05:12

results matching ""

    No results matching ""