1、原理:SpringApplication类中的run(String... args)一路跟进...需查看源码。最终可看到:SpringBoot可通过命令行设置参数。
image.png
2、父工程pom文件,不像Maven打包的方式,需要定义一个替换符
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>local</id>
<properties>
<spring.active>local</spring.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<spring.active>dev</spring.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.active>prod</spring.active>
</properties>
</profile>
</profiles>
3、子工程
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
4、application.yml(以---进行环境拆分)
server:
port: 9090
---
server:
port: 9090
spring:
profiles: local
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
filters: log4j,wall,mergeStat
hikari:
readOnly: false
connectionTimeout: 60000
idleTimeout: 60000
validationTimeout: 3000
maxLifetime: 60000
loginTimeout: 5
maximumPoolSize: 60
minimumIdle: 10
redis:
database: 0
host: 127.0.0.1
port: 6379
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
---
server:
port: 9090
spring:
profiles: produce
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
filters: log4j,wall,mergeStat
hikari:
readOnly: false
connectionTimeout: 60000
idleTimeout: 60000
validationTimeout: 3000
maxLifetime: 60000
loginTimeout: 5
maximumPoolSize: 60
minimumIdle: 10
redis:
database: 0
host: 127.0.0.1
port: 6379
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
5、以拆分文件的形式也可以
application.yml
server:
port: 9090
application-local.yml
server:
port: 9090
spring:
profiles: local
datasource:
url: jdbc:mysql://127.0.0.1:3306/light_tool?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
filters: log4j,wall,mergeStat
hikari:
readOnly: false
connectionTimeout: 60000
idleTimeout: 60000
validationTimeout: 3000
maxLifetime: 60000
loginTimeout: 5
maximumPoolSize: 60
minimumIdle: 10
redis:
database: 0
host: 127.0.0.1
port: 6379
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
application-prod.yml
server:
port: 9090
spring:
profiles: prod
datasource:
url: jdbc:mysql://127.0.0.1:3306/light_tool?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
filters: log4j,wall,mergeStat
hikari:
readOnly: false
connectionTimeout: 60000
idleTimeout: 60000
validationTimeout: 3000
maxLifetime: 60000
loginTimeout: 5
maximumPoolSize: 60
minimumIdle: 10
redis:
database: 0
host: 127.0.0.1
port: 6379
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
6、idea运行方式
image.png
7、打包及运行
package -Dmaven.test.skip
nohup java -jar xxx.jar --spring.profiles.active=prod >/dev/null 2>&1 &
网友评论