媒介
写上一篇看英文资料,淹灭了心力呀,这章,相对来说简朴点。也较量熟悉,可是这很实用。不扯了,开始~
多情况设置
在开拓应用时,常用陈设的应用是多个的,好比:开拓、测试、联调、出产等差异的应用情况,这些应用情况都对应差异的设置项,好比swagger一般上在出产时是封锁的;差异情况数据库地点、端标语等都是不尽沟通的,要是没有多情况的自由切换,陈设起来是很繁琐也容易堕落的。
maven的多情况设置
在没有利用过springboot的多情况设置时,原先是操作maven的profile成果举办多情况设置,这里我简朴回首下。
maven设置
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pom.port>8080</pom.port>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<pom.port>8888</pom.port>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<!-- 插手此属性,才会举办过滤 -->
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<!-- 需要插手,因为maven默认的是${},而springbooot 默认会把此替换成@{} -->
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后编译时,插手-Ptest,昆山软件开发,则会替换test情况下的参数值。 完整参数:
mvn clean install -DskipTests -Ptest
application.properties
server.port=${pom.port}
操作maven实现多情况设置,较量贫苦的就是每次陈设新情况时,都需要再次指定情况编译打包一次。一下进入主题,springboot的多情况,较量优雅了很多。
springboot多情况设置
Profile是Spring针对差异情况差异设置的支持。需要满意application-{profile}.properties,{profile}对应你的情况标识。如:
application-dev.properties:开拓情况application-test.properties:测试情况苏州软件公司 =test 此时就会加载 application-test.properties 的设置内容" class="aligncenter size-full wp-image-29369" title="14742268" src="/uploads/allimg/c180802/153315394LX60-1C18.png" />
而指定执行哪份设置文件,只需要在application.properties设置spring.profiles.active为对应${profile}的值。
# 指定情况为dev spring.profiles.active=dev
则会加载:application-dev.properties的设置内容。
2018-07-15 14:52:41.304 INFO 15496 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-07-15 14:52:41.310 INFO 15496 --- [ main] c.l.l.s.chapter5.Chapter5Application : Started Chapter5Application in 8.506 seconds (JVM running for 10.81) 2018-07-15 14:52:41.316 INFO 15496 --- [ main] c.l.l.s.chapter5.Chapter5Application : 多情况应用启动.
还可以在**呼吁行方法**激活差异情况设置,如
java -jar xxx.jar --spring.profiles.active=test
此时就会加载application-test.properties的设置内容。 test:
苏州软件公司 =test 此时就会加载 application-test.properties 的设置内容" class="aligncenter size-full wp-image-29370" title="80566067" src="/uploads/allimg/c180802/153315394M2040-2R41.png" />
苏州软件公司 =test 此时就会加载 application-test.properties 的设置内容" class="aligncenter size-full wp-image-29371" title="94535535" src="/uploads/allimg/c180802/153315394M4050-31563.png" />