内容简介:一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。关键词:假设,一个应用的工作环境有:dev、test、prod
一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。
关键词: @Profile
、 spring.profiles.active
区分环境的配置
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 复制代码
以上所述就是小编给大家介绍的《SpringBoot 教程之 profile》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Django企业开发实战
胡阳 / 人民邮电出版社 / 2019-2 / 99.00元
本书以博客系统贯穿始末,介绍了Django的方方面面。书中共分四部分,第一部分介绍了正式进入编码之前的准备工作,内容包括需求分析、基础知识和Demo系统的开发;第二部分开始实现需求,内容涉及环境配置、编码规范以及项目结构规划,编写了Model层、admin页面、Form代码和View逻辑,引入了Bootstrap框架;第三部分重点介绍xadmin、django-autocomple-light和d......一起来看看 《Django企业开发实战》 这本书的介绍吧!