今日学习列表
- properties的配置,控制环境
- yml配置环境
- 通过VM虚拟机指定环境
- 通过java启动,并指定环境
properties的配置,控制环境
- application.properties
- application-dev.properties
- application-pro.properties
- application-dog.properties
以上几个文件,如果需要更多环境,替换pro为你想要的,比如那个dog。
application.properties
spring.profiles.active=pro
application-dev.properties
server.port=8082
application-pro.properties
server.port=8083
替换环境,将properties中的pro替换成dev或者dog
yml配置环境
---
spring:
profiles: pro
server:
port: 8091
---
spring:
profiles: dev
server:
port: 8090
---
spring:
profiles:
active: pro
用三个横杠分割,填入以上信息,通过active字段控制运行环境
通过VM虚拟机指定环境
-Dspring.profiles.active=pro
edit configurations,然后添加相关命令,即可定义环境。实测,优先级高于properties的定义规则
通过java启动,并指定环境
点击这个,即可打包。
控制台输出build success后,在target文件路径下即可找到一个jar包。打开终端,输入 java -jar ___
下划线填入jar的路径。那么通过这个也可以启动。
这有一个坑,需要在pom文件中添加build标签,我没有添加,启动jar包的时候,提示错误->
jar中没有主清单属性
pom文件中添加如下的
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这样我们可以启动jar了。
停止jar
mac系统,键盘按Ctrl+c。停止jar任务
暂停jar
mac系统,键盘按Ctrl+z
恢复jar
输入jobs查看被暂停的程序,输入 fg %_ 下划线替换成编号。如果就一个任务暂停,那编号应该是1. 命令就是 fg %1。注意百分号前面的空格
指定环境
java -jar myjar.jar --spring.profiles.active=pro