tomcat中每场战争的不同环境变量 [英] Different environment variables per war in tomcat

查看:105
本文介绍了tomcat中每场战争的不同环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在tomcat中为不同的war文件设置不同的环境变量?我正在使用第三方战争,需要对同一战争进行多次部署,但需要使用不同的环境变量(因此它会加载不同的配置)。

Is there a way to have different environment variables for different war files in tomcat? I am using a 3rd party war and need to have multiple deployments of the same war but with different environment variables (so it loads different configs).

推荐答案

好的,完全疯狂的黑客想法:

Ok, total crazy hack idea:

为每个应用实例实现一个PropertyPlayHolderConfigurer(或者使用Tomcat的web.xml)并加载与你同名的属性拥有System.properties()。

Implement a PropertyPlayHolderConfigurer (or use web.xml from Tomcat) for each app instance and load properties same name as you have for System.properties().

然后,创建一个包含两组属性的委托属性类。然后

Then, create a delegate Properties class that contains both sets of properties. Then

Properties props = new DelegatingProperties(app1Props,app2Props)
System.setProperties(delegate);

public class DelegatingProperties extends Properties {

   private Properties app1Props;
   private Properties app2Props;

   public DelegatingProperties(Properties app1Props, Properties app2Props) {
        this.app1Props = app1Props;
        this.app2Props = app2Props;   
   }

   public String getProperty(String prop) {
       // begin crazy science
       String caller = new Throwable().getStackTrace()[0].getClassName();
       // this is where you get creative.
       // Do the System.setProperties() above so you can intercept calls to  
       //getProperty()
       // and find out the FQCN of the library class(es) that need these variable 
       // (use your debugger).
       // then implement the logic here to decide which of the 2 property sets you have
       // you will query to get the correct results     
   }
}

这些是我们正在讨论的 SYSTEM 属性,它们适用于系统范围。您的图书馆可能是在1-app-1-jvm时开发的(或者开发人员也很可能)。

These are SYSTEM properties we are talking about and they are meant to apply system wide. Your library was probably developed when it was 1-app-1-jvm (or the developer is a tard which is also likely).

我能否至少获得创造力的道具? :)

Can I atleast get props for creativity? :)

这篇关于tomcat中每场战争的不同环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆