我正在尝试在Spring Boot应用程序中读取外部属性文件。目前,该过程运行良好。根据我的要求更改,我需要在旅途中设置一些属性,该属性应在应用程序中自动重新加载,而无需手动重新启动tomcat。目前,我有两个POJO用于​​属性文件,并且在Spring之前已初始化。我打电话给我的休息服务时想手动初始化它。我创建了@Refreshscope,但无法正常工作。

AppController.java

//initial code segments
public class AppController {

LocalProperties localProperties;

@PostMapping(value = "/getdata", produces = "application/json")
    public String getResponse(@RequestHeader HttpHeaders headers, @RequestBody String request) {
       //Somthing like below method to initilize external properties
        loadExternalPropeteries();
        //use the property classes in bussiness logic
}

    private void loadExternalPropeteries() {
        //Assuming the the object wil be created now
        localProperties=new LocalProperties();
    }
}


目前物业的POJO
LocalProperties.java

@PropertySource("file:${spring.config.location}/localConfig.properties")
@ConfigurationProperties(ignoreUnknownFields = false)
public class LocalProperties {
    @Value("${server.url}")
    private String url;
}

最佳答案

尝试遵循https://www.baeldung.com/spring-reloading-properties#reloading-cloud的指示

10-01 03:44