SpringBoot配置文件与热部署

多配置文件存放位置



配置文件可以在项目中的位置如下图:


注意:名称都是application,配置文件的优先级与图中序号一致。

多阶段配置文件

1.使用properties类型的配置文件,这种方式每个阶段需要有一个独立的配置文件,如下图:


2.使用yml类型的配置文件,由于yml配置文件支持分块,每个文档块相当于一个独立的配置文件,所以只需要一个application.yml文件就足够了。

对应代码如下:

# 指定要使用的配置文件
spring:
  profiles:
    active: test
server:
  port: 8081

# --- 表示分块,每块代表一个独立的配置文件
---
#给分块的配置文件命名
spring:
  profiles: test
server:
  port: 8082

---
spring:
  profiles: dev
server:
  port: 8083

热部署

1,首先引入devtools

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.release</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.henu</groupId>
    <artifactId>environment</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>environment</name>
    <description>config</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--引入web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--引入Thymeleaf模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--对插件的管理-->
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2, 配置IDEA

进入设置,找到Compiler,把图中的内容全局勾选,主要是Build project automatically


快捷键 ctrl + shift + alt + / ,   mac上为 shift+option+command+/

选择Registry,勾上 Compiler autoMake allow when app running


3,修改运行参数

修改运行的参数,把On 'Update' action 和On frame deactivation 中的选项都改成Update classes and resources。


注意:这里的热部署其实是热启动,真正的热部署需要使用Jrebel ,这是一个收费的插件。

完成以上热部署之后测试结果为:

  • . 修改类–>保存:应用会重启
  • . 修改配置文件–>保存:应用会重启
  • . 修改页面–>保存:应用不会重启,但会重新加载,页面会刷新


注意:spring-boot-devtools 默认关闭了模版缓存,如果使用这种方式不用单独配置关闭模版缓存。


全部评论