SPRING BOOT TUTORIAL SPRING BOOT TUTORIAL
GitHub (opens new window)
GitHub (opens new window)
  • 框架

    • SpringBoot

      • SpringBoot综合

      • SpringBoot基础

        • SpringBoot 之快速入门
        • SpringBoot 之属性加载详解
        • SpringBoot 之 Profile
          • 区分环境的配置
            • properties 配置
            • yml 配置
          • 区分环境的代码
            • 修饰类
            • 修饰注解
            • 修饰方法
          • 激活 profile
            • 插件激活 profile
            • main 方法激活 profile
            • jar 激活 profile
            • 在 Java 代码中激活 profile
          • 示例源码
          • 参考资料
      • SpringBootWeb

      • SpringBoot数据

      • SpringBootIO

      • SpringBoot安全

      • SpringBoot其他

  • Java
  • 框架
  • SpringBoot
  • SpringBoot基础
dunwu
2019-11-18
目录

SpringBoot 之 Profile

# SpringBoot 之 Profile

一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。

关键词: @Profile、spring.profiles.active

# 区分环境的配置

# properties 配置

假设,一个应用的工作环境有:dev、test、prod

那么,我们可以添加 4 个配置文件:

  • applcation.properties - 公共配置
  • application-dev.properties - 开发环境配置
  • application-test.properties - 测试环境配置
  • application-prod.properties - 生产环境配置

在 applcation.properties 文件中可以通过以下配置来激活 profile:

spring.profiles.active = test

# yml 配置

与 properties 文件类似,我们也可以添加 4 个配置文件:

  • applcation.yml - 公共配置
  • application-dev.yml - 开发环境配置
  • application-test.yml - 测试环境配置
  • application-prod.yml - 生产环境配置

在 applcation.yml 文件中可以通过以下配置来激活 profile:

spring:
  profiles:
    active: prod

此外,yml 文件也可以在一个文件中完成所有 profile 的配置:

# 激活 prod
spring:
  profiles:
    active: prod
# 也可以同时激活多个 profile
# spring.profiles.active: prod,proddb,prodlog
---
# dev 配置
spring:
  profiles: dev

# 略去配置

---
spring:
  profiles: test

# 略去配置

---
spring.profiles: prod
spring.profiles.include:
  - proddb
  - prodlog

---
spring:
  profiles: proddb

# 略去配置

---
spring:
  profiles: prodlog
# 略去配置

注意:不同 profile 之间通过 --- 分割

# 区分环境的代码

使用 @Profile 注解可以指定类或方法在特定的 Profile 环境生效。

# 修饰类

@Configuration
@Profile("production")
public class JndiDataConfig {

    @Bean(destroyMethod="")
    public DataSource dataSource() throws Exception {
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
    }
}

# 修饰注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

# 修饰方法

@Configuration
public class AppConfig {

    @Bean("dataSource")
    @Profile("development")
    public DataSource standaloneDataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:com/bank/config/sql/schema.sql")
            .addScript("classpath:com/bank/config/sql/test-data.sql")
            .build();
    }

    @Bean("dataSource")
    @Profile("production")
    public DataSource jndiDataSource() throws Exception {
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
    }
}

# 激活 profile

# 插件激活 profile

spring-boot:run -Drun.profiles=prod

# main 方法激活 profile

--spring.profiles.active=prod

# jar 激活 profile

java -jar -Dspring.profiles.active=prod *.jar

# 在 Java 代码中激活 profile

直接指定环境变量来激活 profile:

System.setProperty("spring.profiles.active", "test");

在 Spring 容器中激活 profile:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();

# 示例源码

示例源码:spring-boot-profile (opens new window)

# 参考资料

  • Spring 官方文档之 Bean Definition Profiles (opens new window)
  • Spring Boot 官方文档之 boot-features-profiles (opens new window)
📝 帮助改善此页面! (opens new window)
#Java#框架#Spring#SpringBoot
上次更新: 2024/01/27, 23:16:10
SpringBoot 之属性加载详解
SpringBoot 之应用 EasyUI

← SpringBoot 之属性加载详解 SpringBoot 之应用 EasyUI→

最近更新
01
SpringBoot 之快速入门
12-10
02
SpringBoot 之安全快速入门
05-13
03
SpringBoot 知识图谱
08-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 钝悟(dunwu) | CC-BY-SA-4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×