详解Spring Boot Profiles 配置和使用
作者:fanlychie 时间:2021-10-05 22:54:57
介绍
Spring Profiles 提供了一套隔离应用配置的方式,不同的 profiles 提供不同组合的配置,在不同的环境中,应用在启动时通过选择激活某些特定的 profiles 来适应运行时环境,以达到在不同的环境可以使用相同的一套程序代码。
环境
JDK 8
Maven 3
IntelliJ IDEA 2016
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot2.x整合tkmapper的示例代码
SpringMVC请求流程源码解析
![](https://img.aspxhome.com/file/2023/3/64533_0s.jpg)
Java特性队列和栈的堵塞原理解析
Android实现腾讯新闻的新闻类别导航效果
![](https://img.aspxhome.com/file/2023/3/85973_0s.png)
java 汉诺塔Hanoi递归、非递归(仿系统递归)和非递归规律 实现代码
java控制台打印本月的日历
![](https://img.aspxhome.com/file/2023/1/58621_0s.jpg)
Flutter 状态管理的实现
![](https://img.aspxhome.com/file/2023/0/108330_0s.png)
java 发送http和https请求的实例
java.math.BigDecimal的用法及加减乘除计算
SrpingDruid数据源加密数据库密码的示例代码
![](https://img.aspxhome.com/file/2023/1/63021_0s.png)
java poi导出图片到excel示例代码
![](https://img.aspxhome.com/file/2023/6/58736_0s.png)
详解Flutter中视频播放器插件的使用教程
![](https://img.aspxhome.com/file/2023/9/83519_0s.jpg)
解决SpringBoot框架因post数据量过大没反应问题(踩坑)
自己动手写的mybatis分页插件(极其简单好用)
Java开发学习 Eclipse项目有红感叹号解决之道
阿里、华为、腾讯Java技术面试题精选
![](https://img.aspxhome.com/file/2023/4/59904_0s.jpg)
详解spring cloud ouath2中的资源服务器
![](https://img.aspxhome.com/file/2023/8/61238_0s.png)
利用maven引入第三方jar包以及打包
![](https://img.aspxhome.com/file/2023/0/59010_0s.jpg)
JDBC环境设置(中文详解)
IDEA 2020 本土化,真的是全中文了(真香)
![](https://img.aspxhome.com/file/2023/6/60196_0s.png)