本人免费整理了Java高级资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G,需要自己领取。
传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q

一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。
关键词: @Profilespring.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的更多相关文章

  1. Spring Boot中的配置

    一.首先使用idea中的Spring Initializr快速创建一个SpringBoot应用,idea会联网自动创建,创建好的结构如下(一些没必要的文件都删了): 其中说一下几个文件夹和文件 sta ...

  2. Spring Boot(二):Spring Boot中的配置参数

    Spring Boot 配置参数 Spring Boot 帮助我们完成了许许多多的自动化配置 如果我们需要根据自己的需求修改配置 也是可以的 可以使用.properties 和 .yml 格式配置 这 ...

  3. Spring Boot中如何配置线程池拒绝策略,妥善处理好溢出的任务

    通过之前三篇关于Spring Boot异步任务实现的博文,我们分别学会了用@Async创建异步任务.为异步任务配置线程池.使用多个线程池隔离不同的异步任务.今天这篇,我们继续对上面的知识进行完善和优化 ...

  4. spring boot 中active的profile会和标准配置合并吗

    如下图,两个profile配置文件,一个默认的(application.properties),一个是test的. 活跃配置为test. spring.profiles.active=test ste ...

  5. Spring Boot 中的静态资源到底要放在哪里?

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...

  6. spring boot日志管理配置

    spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,Log4J2和Logback.每种L ...

  7. Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...

  8. 自定义spring boot的自动配置

    文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...

  9. Spring Boot中的静态资源文件

    Spring Boot中的静态资源文件 1.SSM中的配置 2.Spring Boot 中的配置 2.1 整体规划 2.2 源码解读 2.3 自定义配置 2.3.1 application.prope ...

随机推荐

  1. Asia Yokohama Regional Contest 2018 G题 What Goes Up Must Come Down

    链接 G题 https://codeforces.com/gym/102082 使其成为单峰序列需要交换多少次相邻的数. 树状数组维护逆序对. 对于每个序列中的数,要么在单峰的左侧,要么在单峰的右侧, ...

  2. 解决CentOS7 Local time比实际时间相差8小时

    GPS系统中有两种时间区分,UTC就0时区的时间,CST为本地时间,如北京为早上八点(东八区),UTC时间比北京时晚八小时; CST:China Standard Time,UTC+8:00 中国沿海 ...

  3. 【ASP.NET Core学习】基础

    新建项目时,程序入口调用CreateDefaultBuilder(args),下面是源代码 public static IHostBuilder CreateDefaultBuilder(string ...

  4. WinForms项目升级.Net Core 3.0之后,没有WinForm设计器?

    目录 .NET Conf 2019 Window Forms 设计器 .NET Conf 2019 2019 9.23-9.25召开了 .NET Conf 2019 大会,大会宣布了 .Net Cor ...

  5. Python:多态、协议和鸭子类型

    多态 问起面向对象的三大特性,几乎每个人都能对答如流:封装.继承.多态.今天我们就要来说一说 Python 中的多态. 所谓多态:就是指一个类实例的相同方法在不同情形有不同表现形式.多态机制使具有不同 ...

  6. [转]Outlook 2016 will not display Web linked images

    本文转自:https://community.spiceworks.com/topic/1952626-outlook-2016-will-not-display-web-linked-images ...

  7. SQL server利用脚本添加链接服务器,可设置别名

    USE [master]GO EXEC master.dbo.sp_addlinkedserver @server = N'你的别名', @srvproduct=N'', @provider=N'SQ ...

  8. 微信小程序开发——上传代码片段到git仓库

    微信开发者工具除了自带的git版本管理(本地服务)之外,还可以推送到在线git仓库中去,这样别人也可以通过git来拉取你的代码片段或小程序. 一.1.登录git 一.2.点击创建项目  一.3.填写项 ...

  9. 苏州市java岗位的薪资状况(2)

    上一篇已经统计出了起薪最高的top 10: 接着玩,把top 10 中所有职位的详细信息爬取下来.某一职位的详情是这样: 我们需要把工作经验.学历.职能.关键字爬取下来. from urllib.re ...

  10. Codeforces Round #593 (Div. 2)

    传送门 A. Stones 签到. B. Alice and the List of Presents 单独考虑每个数的贡献即可. 答案为\((2^{m}-1)^n\). C. Labs 构造就类似于 ...