上个月,我们遇到了类似的情况,客户端需要在运行时更新配置文件。 我们创建使用@Value
@Component
public class ConfigurationManagerComponent{
@Value("conf.username")
private String userName;
@Value("conf.email");
private String email;
/*
* All your attributes
*/
//getters and setters
public String getUserName(){
return this.userName;
}
public void setUserName(String userName){
// first set the value to this class (component)
this.userName=userName;
//second save this value in the properties file
//a specific method you have to implement
saveproperties("conf.username",userName);
}
/*
* Do like this for all your getters and setters
*/
}
然后,在任何时候,你需要获得或保存特性,只是注入该组件并使用它的getter和setter
@Autowired
ConfigurationManagerComponent configComponent;
String myActualUserName=configComponent.getUserName();
中央Spring组件加载从文件中的所有属性
所以你可以从你的控制器获取你的所有数据,并将它们设置为你的弹簧模型以填充网页中的表单,并且在编辑后保存时,你总是可以使用你的配置组件的setter(更新类和属性文件)。
这种方法不是一个标准,只是我们用来解决问题的一种解决方案,它现在给出了很好的结果。 你也可以检查Apache公共配置Here其他解决方案.. 希望这会有所帮助。
对于您的应用程序,您可以在加载属性文件后将更新的属性存储到Map/Dto,并且如果需要可以从那里读取它。当您使用属性文件绑定表单时,您需要确保您正在更新map/dto和属性。您只能在应用程序启动期间使用@Value。在上下文加载后它不会有用。 – asg
嗨,我对你提出的想法感兴趣。你能否有任何我可以涉及的材料或例子?提前致谢! –
请检查此.. https://stackoverflow.com/questions/42179664/inject-all-keys-and-values-from-property-file-as-map-in-spring – asg