Spring Profiles and @Profile

1. Overview

Profiles are a core feature of the framework — allowing us to map our beans to different profiles — for example, dev, test, and prod.

How to set Profiles for Spring
How to active Profiles in Spring
How to get active Profiles in Spring


Profiles in Spring Boot

Spring Boot supports all the profile configuration outlined so far, with a few additional features.

1. Activating or Setting a Profile

# 方式1: 配置文件里配置
spring.profiles.active=dev

# 方式2:使用SpringApplication
SpringApplication.setAdditionalProfiles("dev");

# 方式3:
# Set profiles using Maven in Spring Boot
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <profiles>
                <profile>dev</profile>
            </profiles>
        </configuration>
    </plugin>
    ...
</plugins>
# and execute the Spring Boot-specific Maven goal:
mvn spring-boot:run

2. Profile-specific Properties Files

The most important profiles-related feature that Spring Boot brings is profile-specific properties files. These have to be named in the format application-{profile}.properties.

For example, we can configure different data sources for dev and production profiles by using two files named application-dev.properties and application-production.properties:

# In the application-production.properties file, we can set up a MySql data source:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=root

# In the application-dev.properties file, to use an in-memory H2 database:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

In this way, we can easily provide different configurations for different environments.

3. Multi-Document Files

Club all the properties in the same file and use a separator to indicate the profile
Starting version 2.4, We can specify the dev and production properties in the same application.properties:

my.prop=used-always-in-all-profiles
#---
spring.config.activate.on-profile=dev
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=root
#---
spring.config.activate.on-profile=production
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

This file is read by Spring Boot in top to bottom order.

参考:Spring Profiles

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值