详解Spring Boot Profiles 配置和使用

作者:fanlychie 时间:2021-10-05 22:54:57 

介绍

Spring Profiles 提供了一套隔离应用配置的方式,不同的 profiles 提供不同组合的配置,在不同的环境中,应用在启动时通过选择激活某些特定的 profiles 来适应运行时环境,以达到在不同的环境可以使用相同的一套程序代码。

环境

  1. JDK 8

  2. Maven 3

  3. IntelliJ IDEA 2016

  4. Spring Boot 1.5.2.RELEASE

@Profiles

你可以在任何 @Component(@Service,@Repository) 或 @Configuration 注解标注的类中使用 @Profiles 注解:


public interface PaymentService {
 String createPaymentQrcode();
}

@Service
@Profile("alipay")
public class AlipayService implements PaymentService {
 @Override
 public String createPaymentQrcode() {
   return "支付宝支付二维码";
 }
}

@Service
@Profile({"default", "wechatpay"})
public class WechatpayService implements PaymentService {
 @Override
 public String createPaymentQrcode() {
   return "微信支付二维码";
 }
}

在 Spring Boot 中,默认的 profile 是 default,因此,PaymentService.createPaymentQrcode() -> 微信支付二维码。

你可以通过 spring.profiles.active 来激活某个特定 profile:


java -jar -Dspring.profiles.active='alipay' xxx.jar

PaymentService.createPaymentQrcode() -> 支付宝支付二维码。

多环境配置

在Spring Boot 中,多环境配置文件可以使用 application-{profile}.{properties|yml} 的方式。


@Component
@ConfigurationProperties("jdbc")
public class JdbcProperties {
 private String username;
 private String password;
 // getters and setters
}

开发环境 application-dev.properties 配置:


jdbc.username=root
jdbc.password=123654

生产环境 application-prod.properties 配置:


jdbc.username=produser
jdbc.password=16888888

或:

开发环境 application-dev.yml 配置:


jdbc:
username: root
password: 123654

生产环境 application-prod.yml 配置:


jdbc:
username: produser
password: 16888888

或:

只使用 application.yml,并在此文件中通过 --- 分隔符创建多 profile 配置:


app:
version: 1.0.0
spring:
profiles:
 active: "dev"
---
spring:
profiles: dev
jdbc:
username: root
password: 123654
---
spring:
profiles: prod
jdbc:
username: produser
password: 16888888

命令行启动:


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

来源:http://fanlychie.github.io/post/spring-boot-profiles-usage.html?utm_source=tuicool&utm_medium=referral

标签:Spring,Boot,Profile
0
投稿

猜你喜欢

  • springboot2.x整合tkmapper的示例代码

    2021-09-19 11:56:59
  • SpringMVC请求流程源码解析

    2021-08-07 03:35:11
  • Java特性队列和栈的堵塞原理解析

    2023-10-13 14:15:55
  • Android实现腾讯新闻的新闻类别导航效果

    2023-07-29 04:17:46
  • java 汉诺塔Hanoi递归、非递归(仿系统递归)和非递归规律 实现代码

    2023-09-13 11:29:31
  • java控制台打印本月的日历

    2023-10-15 22:58:12
  • Flutter 状态管理的实现

    2023-08-21 02:38:33
  • java 发送http和https请求的实例

    2023-11-29 12:46:52
  • java.math.BigDecimal的用法及加减乘除计算

    2022-01-15 15:55:20
  • SrpingDruid数据源加密数据库密码的示例代码

    2021-06-21 03:26:26
  • java poi导出图片到excel示例代码

    2023-10-30 00:13:17
  • 详解Flutter中视频播放器插件的使用教程

    2023-06-15 23:47:31
  • 解决SpringBoot框架因post数据量过大没反应问题(踩坑)

    2023-11-28 11:59:30
  • 自己动手写的mybatis分页插件(极其简单好用)

    2023-11-01 18:12:09
  • Java开发学习 Eclipse项目有红感叹号解决之道

    2022-10-22 15:29:27
  • 阿里、华为、腾讯Java技术面试题精选

    2023-11-25 02:29:39
  • 详解spring cloud ouath2中的资源服务器

    2022-09-24 15:36:43
  • 利用maven引入第三方jar包以及打包

    2023-11-15 04:23:17
  • JDBC环境设置(中文详解)

    2021-10-16 21:27:16
  • IDEA 2020 本土化,真的是全中文了(真香)

    2023-11-25 08:02:58
  • asp之家 软件编程 m.aspxhome.com