Действия, которые я делаю:
- Я вызываю http://localhost:8888/api/message, чтобы проверить текущее значение свойства.
- Я редактирую файл application.properties.
< li>Я вызываю http://localhost:8888/actuator/refresh. - Я снова вызываю /api/message, но значение не обновляется. И в журналах, когда вызывается @PreDestroy, я также вижу старое значение.
Любая помощь будет оценена по достоинству
Ниже приведен код:
ConfigProperties.java
package org.example.refreshscopeworkingproject;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "demo")
@RefreshScope
public class ConfigProperties {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@PostConstruct
public void init() {
System.out.println("ConfigProperties bean created with message: " + message);
}
@PreDestroy
public void destroy() {
System.out.println("ConfigProperties destroyed");
}
}
DemoController.java
package org.example.refreshscopeworkingproject;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
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;
@RestController
@RequestMapping("/api")
@RefreshScope
public class DemoController {
private final ConfigProperties configProperties;
public DemoController(ConfigProperties configProperties) {
this.configProperties = configProperties;
}
@GetMapping("/message")
public String getMessage() {
return configProperties.getMessage();
}
@PostConstruct
public void init() {
System.out.println("Controller initialized");
}
@PreDestroy
public void destroy() {
System.out.println("Controller destroyed");
}
}
ОбновитьScopeWorkingProjectApplication.java
package org.example.refreshscopeworkingproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RefreshScopeWorkingProjectApplication {
public static void main(String[] args) {
SpringApplication.run(RefreshScopeWorkingProjectApplication.class, args);
}
}
application.properties:
demo.message=Hello, RefreshScope1111!
management.endpoints.web.exposure.include=*
logging.level.org.springframework.boot.actuate=DEBUG
logging.level.org.springframework.boot.context.properties=DEBUG
server.port=8888
pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.3.4
org.example
RefreshScopeWorkingProject
0.0.1-SNAPSHOT
RefreshScopeWorkingProject
RefreshScopeWorkingProject
17
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-context
4.0.3
org.springdoc
springdoc-openapi-starter-webmvc-ui
2.6.0
org.springframework.boot
spring-boot-maven-plugin
Подробнее здесь: https://stackoverflow.com/questions/791 ... properties
Мобильная версия