我正在尝试在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 business 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;
}