Spring Boot 中如何配置 Profile
本人免费整理了Java高级资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G,需要自己领取。
传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q
一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。
关键词:@Profile、spring.profiles.active
目录
- 区分环境的配置
- properties 配置
- yml 配置
- 区分环境的代码
- 修饰类
- 修饰注解
- 修饰方法
- 激活 profile
- 插件激活 profile
- main 方法激活 profile
- jar 激活 profile
- 在 Java 代码中激活 profile
区分环境的配置
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();
使用方法:
mvn clean package
cd target
java -jar -Dspring.profiles.active=prod sbe-core-profile.jar
Spring Boot 中如何配置 Profile的更多相关文章
- Spring Boot中的配置
一.首先使用idea中的Spring Initializr快速创建一个SpringBoot应用,idea会联网自动创建,创建好的结构如下(一些没必要的文件都删了): 其中说一下几个文件夹和文件 sta ...
- Spring Boot(二):Spring Boot中的配置参数
Spring Boot 配置参数 Spring Boot 帮助我们完成了许许多多的自动化配置 如果我们需要根据自己的需求修改配置 也是可以的 可以使用.properties 和 .yml 格式配置 这 ...
- Spring Boot中如何配置线程池拒绝策略,妥善处理好溢出的任务
通过之前三篇关于Spring Boot异步任务实现的博文,我们分别学会了用@Async创建异步任务.为异步任务配置线程池.使用多个线程池隔离不同的异步任务.今天这篇,我们继续对上面的知识进行完善和优化 ...
- spring boot 中active的profile会和标准配置合并吗
如下图,两个profile配置文件,一个默认的(application.properties),一个是test的. 活跃配置为test. spring.profiles.active=test ste ...
- Spring Boot 中的静态资源到底要放在哪里?
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...
- spring boot日志管理配置
spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,Log4J2和Logback.每种L ...
- Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...
- 自定义spring boot的自动配置
文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...
- Spring Boot中的静态资源文件
Spring Boot中的静态资源文件 1.SSM中的配置 2.Spring Boot 中的配置 2.1 整体规划 2.2 源码解读 2.3 自定义配置 2.3.1 application.prope ...
随机推荐
- IT兄弟连 HTML5教程 设置IE9以下版本浏览器支持HTML5
HTML2.HTML5刚发布时由于各浏览器之间的标准不统一,开发者的时间都浪费在解决Web浏览器之间的兼容性上.但由于W3C和WHATWG对HTML5新版本的制定,以及近年来对HTML5的使用,再加上 ...
- IOS系统input输入框为readonly时, 隐藏键盘上的上下箭头
业务中在一定场景中会将input 设置为只读状态,在IOS safari上当input 输入框focus 时,仍会出现键盘上的上下箭头,这种用户体验非常不好,如何干掉呢? <input read ...
- 动态数组原理【Java实现】(六)
前言 接下来我们进入集合学习,看过很多文章一上来就是讲解原理感觉会特别枯燥,任何成熟解决方案的出现都是为了解决问题,若通过实际问题引入然后再来讲解原理想必学起来必定事半功倍,从我写博客的那一天起,我就 ...
- 为什么老外不愿意用MyBatis?
作者:陈龙 www.zhihu.com/question/309662829 Spring 团队的Josh Long自己在Twitter上做了一个调查.1625次投票,样本量不算大,但也能说明问题.和 ...
- cocos2d-x 新工程的把玩
创建了cocos的工程以及初步了解了工程的结构之后,可以尝试自己改改代码了 游戏窗口的设置 首先是AppDelegate,找到AppDelegate.cpp中AppDelegate::applicat ...
- [Spring cloud 一步步实现广告系统] 1. 业务架构分析
什么是广告系统? 主要包含: 广告主投放广告的<广告投放系统> 媒体方(广告展示媒介-)检索广告用的<广告检索系统> 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用 ...
- WPF customize DelegateCommand
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...
- VUE项目中文件上传兼容IE9
项目使用VUE编写,UI是ElementUI,但是Element的Upload组件是不兼容IE9的.因为IE9中无法使用FormData. 查找资料基本有两种解决方法:1.引入JQuery和jQuer ...
- Oracle 备份数据库
[目录] ①备份数据库(https://www.cnblogs.com/xqz0618/p/oracle_backup.html) ②定时备份数据库(https://www.cnblogs.com/x ...
- Django2.1集成xadmin管理后台所遇到的错误集锦,解决填坑(二)
django默认是有一个admin的后台管理模块,但是丑,功能也不齐全,但是大神给我们已经集成好了xadmin后台,我们拿来用即可,但是呢,django已经升级到2.1版本了,xadmin貌似跟不上节 ...