Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ RefreshScope annotations in Springboot to achieve dynamic loading of configuration files

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Springboot how to use @ RefreshScope annotations to achieve dynamic loading of configuration files", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "Springboot how to use @ RefreshScope annotations to achieve dynamic loading of configuration files" this article.

Spring-boot-starter-actuator provides service health check and exposure of the built-in url interface.

Spring-cloud-starter-config provides some support and annotations for dynamic refresh.

Pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.6 com.xiaobu demo-for-mybatis-plus 0.0.1-SNAPSHOT demo-for-mybatis-plus demo-for-mybatis-plus 1.8 2020.0.3 org.springframework.boot Spring-boot-starter-web org.springframework.boot spring-boot-starter-test test asm org.ow2.asm com.baomidou mybatis -plus-boot-starter 3.4.2 org.projectlombok lombok 1.16.10 cn.hutool hutool-all 5.3.2 io.springfox springfox-swagger2 2 . 9.2 guava com.google.guava io.springfox springfox-swagger-ui 2.9.2 com.google.guava guava 29.0-jre com.alibaba easyexcel 2.0.2 junit junit com.xuxueli xxl-job-core 2.3.0 mysql mysql-connector -java org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-bootstrap Org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import src/main/resources src/main/java * * / * .xml true App org.springframework.boot spring-boot-maven-plugin 2.4.5 properties# Mybatis since Body configuration # logging.level.com.xiaobu=debugmybatis-plus.type-aliases-package=com.xiaobu.entitymybatis-plus.mapper-locations=classpath:com/xiaobu/mapper/xml/*.xml# console print sql with parameter cannot write file # mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# write sql to file with parameter mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl # configuration of integrated mysql database spring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/master0?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghaispring.datasource.username=rootspring.datasource.password=root# test dynamic refresh configuration order.pay-timeout-seconds=9999order.create-frequency-seconds=600# exposes the built-in refresh configuration file url This must be written, otherwise the configuration file management.endpoints.web.exposure.include=refresh#management.endpoints.web.exposure.include=env,refresh#management.endpoints.web.exposure.include=env,refresh startup class package com.xiaobu cannot be refreshed Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.ConfigurationPropertiesScan;/** * @ author cloth * / @ SpringBootApplication@ConfigurationPropertiesScanpublic class DemoForMybatisPlusApplication {public static void main (String [] args) {SpringApplication.run (DemoForMybatisPlusApplication.class, args);}} configuration class 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@Datapublic class OrderProperties {/ * order payment timeout (in seconds). * / private Integer payTimeoutSeconds; / * order creation frequency (in seconds * / private Integer createFrequencySeconds;} controllerpackage 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 cloth * @ version 1.0.0 * @ className RefreshController.java * @ createTime 15:38:00 * / @ RestController@RequestMapping ("refresh") @ RefreshScopepublic 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 ();}} package

Execution

Mvn clean package-Dmaven.test.skip=true

Cmd starts jar and specifies an external configuration file

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

Visit: http://localhost:8080/refresh/test

Modify the contents of the configuration file:

Execute POST http://localhost:8080/actuator/refresh

Visit again: http://localhost:8080/refresh/test

Visit: http://localhost:8080/refresh/test01

Springboot version corresponding to springcloud

These are all the contents of the article "how Springboot uses @ RefreshScope annotations to achieve dynamic loading of configuration files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report