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 ...
随机推荐
- angularjs $scope与this的区别,controller as vm有何含义?
壹 ❀ 引 初学angularjs的同学对于$scope一定不会陌生,scope(作用域)是将view(视图)与model(模板)关联起来的桥梁,通过controller(控制器)对于model的数 ...
- JS如何判断鼠标滚轮向上还是向下滚动
前几天偶然看到某前端群有人在问:JS如何判断鼠标滚轮向上还是向下滚动? 我想了想,有点蒙蔽,心想难道不是用scrollTop来判断吗? 但我不确定,也出于好奇心,于是开始了一番探索 思路:通过even ...
- MySQL插入数据时报错Cause: java.sql.SQLException: #HY000的解决方法
数据库中有字段要求不能为空,但是insert插入的时候,改字段没有值
- JDK1.8源码分析02之阅读源码顺序
序言:阅读JDK源码应该从何开始,有计划,有步骤的深入学习呢? 下面就分享一篇比较好的学习源码顺序的文章,给了我们再阅读源码时,一个指导性的标志,而不会迷失方向. 很多java开发的小伙伴都会阅读jd ...
- sqlserver查看表空间
sqlserver 用于查看当前数据库所有表占用空间大小的存储过程 create procedure dbo.proc_getsize as begin create table #temp ( t_ ...
- centos7上搭建开源系统jforum
centos7上搭建好tomcat,mysql; 将 jforum-2.6.2.war放到tomcat目录的webapps下: 启动tomcat,./startup.sh ,查看webapp下jfor ...
- Java之封装性
封装概述 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改. 封装可以被认为是一个保护屏障,防止该类的代码和数据被其他类随意访问.要访问该类的数据,必 ...
- ASM与ASMD
时序机 状态机 控制器 数据通路之间的关系 样例代码
- 解决谷歌浏览器在F12情况下自动断点问题(Paused in debugger)
解决谷歌浏览器在F12情况下自动断点问题(Paused in debugger) 最近在使用谷歌浏览器在调试js脚本的时候,每次按F12,再刷新页面,都会跳出如上图所示的图标,自动进入断点调试.如果不 ...
- Sphinx 2.0.8 发布,全文搜索引擎 Installing Sphinx on Windows
参考资料地址信息 http://sphinxsearch.com/docs/latest/installing-windows.html http://my.oschina.net/melonol/b ...