[英]How to replace a property in the deployment.properties of tomcat temporarily from java method?
I have a method that consumes a rest service. 我有一种消耗休息服务的方法。 The URL for this rest service is taken from deployment.properties
of tomcat using org.springframework.core.env.Environment
此休息服务的URL使用org.springframework.core.env.Environment
从tomcat的deployment.properties
中获取
@PropertySource("file:${catalina.home}/conf/deployment.properties")
public class OriginalService{
@Autowired
private Environment env;
public void originalMethod(){
String endPoint = env.getProperty("rest.url");
.
.
}
}
Though I own that original service, I don't want to change anything in that project. 尽管我拥有原始服务,但我不想更改该项目中的任何内容。 I wish to call this method as it is from another java project but, just replace the URL in the above line, so that the method consumes my dummy service instead of original one. 我希望从另一个Java项目中调用此方法,但是只需替换上一行中的URL,这样该方法将消耗我的虚拟服务而不是原始的服务。
Both the projects are deployed on the same tomcat server. 这两个项目都部署在同一台Tomcat服务器上。
Is there any way I can replace the rest.url
property in the deployment.properties
of tomcat temporarily from a java method? 有什么方法可以从Java方法临时替换tomcat的deployment.properties
中的rest.url
属性?
Rather than updating the original file, it can be altered while loading the properties. 加载属性时可以更改原始文件,而不是更新原始文件。 It can be done by overriding the PropertyPlaceHolderConfigurer
class as follows. 可以通过如下重写PropertyPlaceHolderConfigurer
类来完成此操作。
public class MyPropertyConfigurer extends PropertyPlaceHolderConfigurer{
protected void convertProperties(Properties props){
Enumeration<?> propertyNames = props.propertyNames();
while(propertyNames.hasMoreElements()){
String propName = (String)propertyNames.nextElement();
String propValue = (String)props.getProperty(propName);
if(propName.indexOf("rest.url") != -1){
setPropertyValue(props,propName,propValue);
}
}
}
private void setPropertyValue(Properties props,String propName,String propValue){
String newRestUrl = "<your local url>";
props.setProperty(propName,newRestUrl);
}
}
MyPropertyConfigurer
can be registered as any other bean. MyPropertyConfigurer
可以注册为任何其他bean。 I think, this is much more cleaner way. 我认为,这是一种更清洁的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.