租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

spring怎么重新加载配置文件,spring 重新注入

发布时间:2023-11-14 22:50:57

spring怎样重新加载配置文件

在Spring中,可使用@RefreshScope注解来实现重新加载配置文件。

  1. 首先,在需要重新加载配置文件的类上添加@RefreshScope注解,例如:
@RefreshScope
@Component
public class MyBean {
    // ...
}
  1. application.propertiesapplication.yml中添加以下配置:
management.endpoints.web.exposure.include=refresh
  1. 确保Spring Boot利用中已配置了Spring Cloud Config Server或其他配置中心。

  2. 发送POST要求到/actuator/refresh端点来触发配置文件的重新加载。可使用cURL或其他HTTP客户端工具发送要求。例如,使用curl发送要求:

$ curl -X POST http://localhost:8080/actuator/refresh
  1. Spring会重新加载配置文件,并更新被@RefreshScope注解标记的类中的属性值。