springboot中多环境配置

1.主要在pom.xml中做配置,如下:

<build>
 <pluginManagement>
  <plugins>
		<plugin>
		 <groupId>org.springframework.boot</groupId>
		 <artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	 </plugins>
	</pluginManagement>
	<finalName>${project.artifactId}</finalName>
<filters>
            <filter>src/main/resources/env/${env}.properties</filter>
        </filters>
        <resources>
        	<resource>
        	 <!--拷贝并替换属性文件中的占位符,用profile中的环境变量 -->
        	 <directory>src/main/resources</directory>
        	 <includes>
        	       <include>**/*.properties</include>
        	 </includes>
        	 <filtering>true</filtering>
        	</resource>
        </resources>
</build>
	<profiles>
	 <profile>
	  <!-- dev 本地开发测试环境 -->
	  <id>dev</id>
	  <properties>
	   <env>dev</env>
	  </properties>
	  <activation>
	   <activeByDefault>true</activeByDefault>
     </activation>
		</profile>
		<profile>
		 <!-- beta测试环境 -->
		      <id>beta</id>
		 <properties>
		  <env>beta</env>
		 </properties>
		</profile>
		<profile>
		 <!-- 发布环境 -->
		 <id>release</id>
		 <properties>
		  <env>release</env>
		 </properties>
		</profile>
	</profiles>
这是maven的功能,springboot中已经集成了maven的这项动态配置,所以引包时只需要
spring-boot-maven-plugin
。这个功能带来的好处是:1.配置文件位置灵活,在resources下可以另起一个包或者多个包。2.springboot总的配置文件中application.properties的变量值可以使用[@field_name@]占位符,其引用的是其他配置文件的属性内容。

2.如何操作?

执行maven编译命令,mvn package -Pdev,即可。-P后面紧跟环境的id。

详细的每个xml标签解释百度一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值