SpringBoot之分环境配置


声明:本文转载自https://my.oschina.net/wangxu3655/blog/1807821,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

假设项目需要分3个环境配置:dev、test、prd,  对于springboot的项目, 可以使用以下方式:

1, 使用springboot自己的profile

springboot的多环境配置文件名必须约定满足application-{profile}.properties的格式, 如:

然后需要设置spring.profiles.active的值,才可以激活对应的profile.

我们可以在application.properties文件中激活, 如:

spring.profiles.active=dev 

也可以在运行jar包时激活, 会覆盖上面的, 如:

java -jar hello.jar --spring.profiles.active=test 或者 java -Dspring.profiles.active=test -jar hello.jar 

注解@Profile介绍:

在springboot中开发java组件时,可以通过@Profile决定该类是否实例化, @Profile(“dev”)表示只有spring.profiles.active=dev时,才会去实例化当前类 , 可以用在一些场景中, 比如: 在开发测试环节, 需要屏蔽掉发短信接口. 我们可以参考下面的代码:

public interface SmsService {      void send(); }  @Service @Profile({"dev","test"}) public class DevTestSmsServiceImpl implements SmsService {     @Override     public void send() {         System.out.println("开发测试阶段不需要发真实短信");     } }  @Service @Profile("prd") public class PrdSmsServiceImpl implements SmsService {     @Override     public void send() {         System.out.println("调用真实短信接口");     } }

 

2, 使用打包工具的profile

关于springboot的profile,有人会觉得它有以下两点不好:

a) 对配置文件路径和命名的约定, 造成了很大的不灵活性.

b) 把所有配置文件不分环境青红皂白地都打进jar包有点太粗暴.

于是我们可以回到传统的做法上来, 使用打包工具的profile, 在打包阶段即划清各个环境.

maven profilehttp://maven.apache.org/guides/introduction/introduction-to-profiles.html )

maven profile的使用方式有很多, 这里选取常用的一种, 在pom里配置好profile, 在mvn打包时激活.

pom文件, 如:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.wx</groupId>     <artifactId>hello</artifactId>     <version>1.0-SNAPSHOT</version>      <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>1.5.12.RELEASE</version>     </parent>      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>     </dependencies>      <!--定义3个profile-->     <profiles>         <profile>             <id>dev-profile</id>             <activation>                 <property>                     <name>maven.profile.env</name>                     <value>dev</value>                 </property>                 <activeByDefault>true</activeByDefault>             </activation>             <properties>                 <env>dev</env>             </properties>         </profile>          <profile>             <id>test-profile</id>             <activation>                 <property>                     <name>maven.profile.env</name>                     <value>test</value>                 </property>             </activation>             <properties>                 <env>test</env>             </properties>         </profile>           <profile>             <id>prd-profile</id>             <activation>                 <property>                     <name>maven.profile.env</name>                     <value>prd</value>                 </property>             </activation>             <properties>                 <env>prd</env>             </properties>         </profile>      </profiles>      <build>          <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>          <!--定制打包时的资源拷贝-->         <resources>             <resource>                 <directory>src/main/resources</directory>                 <targetPath>./</targetPath>                 <includes>                     <include>application.properties</include>                     <include>application-${env}.properties</include>                 </includes>             </resource>         </resources>      </build>  </project>

然后在mvn打包时设置maven.profile.env的值, 如:

mvn clean package -Dmaven.skip.test=true -Dmaven.profile.env=prd 

则最终的打进jar包里的配置文件就是这样的: 

本文发表于2018年05月07日 01:23
(c)注:本文转载自https://my.oschina.net/wangxu3655/blog/1807821,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2125 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1