简体   繁体   English

如何从Java方法临时替换tomcat的deploy.properties中的属性?

[英]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.

相关问题 如何从Windows命令提示符修改Java控制面板选择(对应于deployment.properties文件)? - How to modify Java Control Panel selections (corresponding to deployment.properties file) from windows command prompt? Java系统级Deployment.properties - Java system level deployment.properties 是否有一个等效于Java Deployment.properties文件的Adobe? - Is there an Adobe equivalent to the Java deployment.properties file? 从Deployment.properties创建隐藏目录 - Create a hidden directory from deployment.properties 使用bat文件编辑Deployment.properties文件 - Editing Deployment.properties file using bat file Maven - 如何用来自 Java class 方法或 ZA832247ZDF45E6D959 的动态值替换文件中的自定义令牌 - Maven - how to replace a custom token in a file with a dynamic value from a Java class method or static property? Java Servlet部署Tomcat - Java Servlet Deployment Tomcat 如何在JAVA中更新/替换Kubernetes的部署 - How to update/replace deployment of Kubernetes in JAVA 带有 tomcat 的简单 Java Web 应用程序,从本地主机到部署 - Simple Java web app with tomcat, from localhost to deployment 从属性文件JAVA TOMCAT中检索密钥 - Retrieve a Key from a properties file JAVA TOMCAT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM