亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

發布時間:2021-09-09 12:48:16 來源:億速云 閱讀:980 作者:小新 欄目:開發技術

這篇文章主要為大家展示了“Springboot如何使用@RefreshScope注解實現配置文件的動態加載”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Springboot如何使用@RefreshScope注解實現配置文件的動態加載”這篇文章吧。

spring-boot-starter-actuator提供服務健康檢查和暴露內置的url接口。

spring-cloud-starter-config提供動態刷新的一些支持和注解。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.4.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiaobu</groupId>
    <artifactId>demo-for-mybatis-plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-for-mybatis-plus</name>
    <description>demo-for-mybatis-plus</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <!--spring boot-->
        <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>
            <exclusions>
                <exclusion>
                    <artifactId>asm</artifactId>
                    <groupId>org.ow2.asm</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!-- lomback -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.2</version>
        </dependency>
        <!-- 引入Swagger2依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--   spring-cloud config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--        springcloud 高版本需要引入 spring-cloud-starter-bootstrap 否則刷新不起效-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>App</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.5</version>
            </plugin>
        </plugins>
    </build>

</project>

properties

########## Mybatis 自身配置 ##########
logging.level.com.xiaobu=debug
mybatis-plus.type-aliases-package=com.xiaobu.entity
mybatis-plus.mapper-locations=classpath:com/xiaobu/mapper/xml/*.xml
# 控制臺打印sql 帶參數 無法寫入文件
#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 將sql 寫入文件 帶參數
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
#集成mysql數據庫的配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/master0?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#測試動態刷新配置
order.pay-timeout-seconds=9999
order.create-frequency-seconds=600
#暴露內置的刷新配置文件url,這個必須寫,否則無法刷新配置文件
management.endpoints.web.exposure.include=refresh
#management.endpoints.web.exposure.include=env,refresh#management.endpoints.web.exposure.include=env,refresh

啟動類

package com.xiaobu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

/**
 * @author 小布
 */
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoForMybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoForMybatisPlusApplication.class, args);
    }

}

配置類

package com.xiaobu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * @author 小布
 */
@Component
@ConfigurationProperties(prefix = "order")
@RefreshScope
@Data
public class OrderProperties {
    /**
     * 訂單支付超時時長,單位:秒。
     */
    private Integer payTimeoutSeconds;

    /**
     * 訂單創建頻率,單位:秒
     */
    private Integer createFrequencySeconds;

}

controller

package com.xiaobu.controller;

import com.xiaobu.config.OrderProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type Refresh controller.
 *
 * @author 小布
 * @version 1.0.0
 * @className RefreshController.java
 * @createTime 2021年09月06日 15:38:00
 */
@RestController
@RequestMapping("refresh")
@RefreshScope
public class RefreshController {

    @Autowired
    private OrderProperties orderProperties;

    @Value(value = "${order.pay-timeout-seconds}")
    private Integer payTimeoutSeconds;

    /**
     * Test string.
     *
     * @return the string
     */
    @GetMapping("test")
    public String test() {
        return "payTimeoutSeconds:" + payTimeoutSeconds;
    }

    @GetMapping("test01")
    public String test01() {
        return orderProperties.toString();
    }
}

打包

執行

mvn clean package -Dmaven.test.skip=true

cmd啟動jar 并指定外部配置文件

java -jar App.jar  --spring.config.location=D:/application.properties

訪問:http://localhost:8080/refresh/test

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

修改配置文件內容:

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

執行 POST http://localhost:8080/actuator/refresh

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

再次訪問:http://localhost:8080/refresh/test

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

訪問:http://localhost:8080/refresh/test01

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

springcloud對應的springboot版本

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

Springboot如何使用@RefreshScope注解實現配置文件的動態加載

以上是“Springboot如何使用@RefreshScope注解實現配置文件的動態加載”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

奉化市| 肃北| 长治市| 临夏市| 华亭县| 望都县| 梅河口市| 怀柔区| 阿拉善右旗| 河池市| 于都县| 三都| 运城市| 郯城县| 泸溪县| 阳泉市| 新民市| 牡丹江市| 茶陵县| 西林县| 九龙坡区| 花莲市| 柯坪县| 沁水县| 龙陵县| 遵义市| 淅川县| 永新县| 安国市| 泸定县| 菏泽市| 达拉特旗| 吉木萨尔县| 子洲县| 聊城市| 安西县| 嫩江县| 丹江口市| 岑巩县| 嘉峪关市| 本溪市|