SpringBoot Profiles配合Maven Profiles以及Filtering实现多环境下配置切换

版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/yh_zeng2 https://blog.csdn.net/yh_zeng2/article/details/82352037

       本文章是上一篇文章《SpringBoot Profiles实现多环境下配置切换》的升华。到底有什么不一样呢?上一篇文章中,切换环境是在启动程序的时候,添加参数--spring.profiles.active实现,如下:

java -jar xxx.jar --spring.profiles.active=test 表示使用测试环境的配置

那么根据本篇文章,可以实现,在Maven打包的时候,只打包指定环境的配置,最终实现的效果如下:

打包发布:

当然也可以勾选环境之后,通过IDE插件运行程序:

通过上图可以看出,切换环境,非常简单。

实现思路简述:

    Maven支持Profile功能,当使用Maven Profile打包时,可以打包指定目录和指定文件,spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一个变量,当在勾选Maven Projects的Profiles的时候,自动触发使用pom.xml的变量值替换掉properties文件中的spring.profiles.active=xxx的变量值即可。

下面就通过demo来讲解,该demo中,只有开发(dev)、测试(test)、生产(prod)三个环境:

对应的是application-dev.properties、application-test.properties和application-prod.properties。

1. 添加特定环境的properties文件

文件的存放如下

 application-dev.properties

#开发环境dev加载的配置文件
spring.profiles=dev
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.database=ORACLE
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
logging.config=classpath:log4j2-dev.xml

   application-test.properties

#测试环境test加载的配置文件
spring.profiles=test
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.database=ORACLE
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
logging.config=classpath:log4j2-test.xml

  application-prod.properties

#生产环境prod加载的配置文件
spring.profiles=prod
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.database=ORACLE
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
logging.config=classpath:log4j2-prod.xml

 由于只是个例子,所以三个环境配置的数据源都是同一个,当然在大部分的企业应用中,都是配置不同的数据源。

2. 使用Maven Profiles

	<!--实现多环境配置打包-->
	<profiles>
		<!--开发环境-->
		<profile>
			<id>dev</id>
	        <properties>
				<active.profile>dev</active.profile>
	        </properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<!-- end -->
	    <!--测试环境-->
		<profile>
			<id>test</id>
			<properties>
				<active.profile>test</active.profile>
			</properties>
		</profile>
		<!-- end -->
		<!--生产环境-->
		<profile>
			<id>prod</id>
			<properties>
				<active.profile>prod</active.profile>
			</properties>
		</profile>
		<!-- end -->		
	</profiles>

3. SpringBoot Profiles、Maven Profiles和Filtering整合在一起使用

3.1 定义properties文件变量的分隔符(标识符)

   修改pom.xml,添加如下配置

			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<!-- 配置资源文件中的变量分隔符(标识符),这里配置了两种变量分隔符 -->
					<delimiters>
						<delimiter>${*}</delimiter> <!-- 以${}为分隔符,例如 ${jdbc.url}  -->
						<delimiter>@</delimiter>    <!-- 以@为分隔符,例如 @jdbc.url@ -->
					</delimiters>
				</configuration>
			</plugin>

3.2 application.properties添加spring.profiles.active属性,属性值为变量,变量值由pom.xml的propfiles的properties属性提供

#-------------------------通用配置,程序启动默认加载,并根据不同的环境(开发、测试、生产)还会加载特定环境下的配置文件--------------
server.port=8081
#配置项目启动的时候,决定使用哪个环境,开发(dev)、测试(test)还是产品(prod)环境
spring.profiles.active=${active.profile}

注意:spring.profiles.active的变量名称必须使用pom.xml中的<profiles>各个环境定义的properties属性名称!

3.3 通过resources实现只打包特定环境的配置文件,以及使用Maven Profiles自动切换Spring Boot的环境配置

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<!-- filtering必须为true,才可以使用环境变量、pom文件里定义的属性和指定配置文件里的属性来替换属性文件(*.properties)里的变量 -->
				<filtering>true</filtering> 
			</resource>
			<!-- Maven只打包src/main/profile目录下的application-${active.profile}.properties和log4j2-${active.profile}.xml文件-->
			<resource>
				<directory>src/main/profiles</directory>
				<includes>
					<include>**/application-${active.profile}.properties</include>
					<include>**/log4j2-${active.profile}.xml</include>
				</includes>
				<!-- filtering必须为true,才可以使用环境变量、pom文件里定义的属性和指定配置文件里的属性来替换属性文件(*.properties)里的变量 -->
				<filtering>true</filtering>
			</resource>
     ...

猜你喜欢

转载自blog.csdn.net/yh_zeng2/article/details/82352037