我有一个项目,我想在运行时更新 application.properties 的属性,并使用新的/更新的值重新创建依赖于配置和配置 bean 本身的 bean。当我调用 http://localhost:8888/actuator/refresh 但值未更新时,Bean 被销毁。 我所做的步骤:
我也尝试过使用@Value注释,但没有成功并且无法找出问题所在。
如有任何帮助,我们将不胜感激:)
下面是代码: 配置属性.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");
}
}
RefreshScopeWorkingProjectApplication.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);
}
}
应用程序属性:
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:
<?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>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>RefreshScopeWorkingProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RefreshScopeWorkingProject</name>
<description>RefreshScopeWorkingProject</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
看起来您正在更改应用程序正在使用的错误的
application.properties
。当您使用 Maven 构建项目时,您的资源和编译的类将放置在 target
文件夹中。这是您需要更改的 application.properties
所在的文件夹。如果您将项目构建为可执行 jar 文件,则 application.properties
将位于该文件内,并且您将无法更改它。在这种情况下,只需在 jar 文件旁边创建一个新的 application.properties
文件,并将所需的属性和新值放入其中即可。外部文件中的设置将覆盖内部文件中的设置,您将能够轻松更改它们。
有关更多信息,请参阅 Spring Boot 文档的外部化配置部分。