tomcat中每场战争的不同环境变量

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

如果您独立运行两个Tomcat实例,则很容易。 我假设你在这里谈论的是OS环境变量。

您还可以在Tomcat中为每个war / web应用程序设置属性。 那会让你在一个Tomcat实例中运行两个战争。 但那不是你问的。

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

为每个应用程序实例实现PropertyPlayHolderConfigurer(或使用Tomcat的web.xml),并加载与System.properties()相同名称的属性。

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

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时开发的(或者开发人员也很可能)。

我能获得创造力的道具吗? 🙂