Tomcat 7 应用程序 Java 环境变量(无 JNDI) [英] Tomcat 7 application Java environment variables (JNDI-less)

查看:31
本文介绍了Tomcat 7 应用程序 Java 环境变量(无 JNDI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Tomcat 7,我需要向我的两个 Web 应用程序传递一个具有相同名称但每个 Web 应用程序具有不同值的环境变量.

I'm running Tomcat 7 and I need to pass my two web applications an environment variable with the same name but with a different value for each web application.

有没有办法在不使用 JNDI 的情况下拥有特定于应用程序的环境变量(每个应用程序的相同变量具有不同的值)?

Is there any way to have application specific environment variables (the same variable has a different value per application) without using JNDI ?

我正在考虑在特定于应用程序的上下文中使用 -Dname=bob 之类的东西.

I'm thinking of something like a -Dname=bob in an application specific context.

推荐答案

为每个应用程序创建一个名为 application war 文件的文件,并将该文件放在 Tomcat 7 conf/Catalina/localhost/目录中.

Create a file per application named like the application war file and have the file sitting in the Tomcat 7 conf/Catalina/localhost/ directory.

每个文件都可以包含一些特定于应用程序的环境变量.

Each file can then contain some environment variables specific to the application.

例如部署在webapps目录下的两个应用project1.war和project2.war.

For example, the two applications project1.war and project2.war deployed in the webapps directory.

在 conf/Catalina/localhost/project1.xml 文件中

In the file conf/Catalina/localhost/project1.xml

<Context path="" docBase="project1">
  <Environment name="product" value="one" type="java.lang.String" override="false" />
</Context>

conf/Catalina/localhost/project2.xml

conf/Catalina/localhost/project2.xml

<Context path="" docBase="project1">
  <Environment name="product" value="two" type="java.lang.String" override="false" />
</Context>

我也没有遇到双重部署.

Also I didn't experience the double deployment.

我的开发服务器主机是:

My development server host is:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" deployOnStartup="true">

我的生产服务器主机是:

And my production server host is:

<Host name="www.learnintouch.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" deployOnStartup="true">

为了检索变量的值,我使用了 Spring 条件上下文:

To retrieve the value of the variable, I use the Spring condition context:

public class ProductProjectCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("product") == null || context.getEnvironment().getProperty("product").equals("project");
    }

}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Conditional(ProductProjectCondition.class)
public @interface ProductProject {
}

然后就可以使用@ProductProject 注释了.

It is then possible to use the @ProductProject annotation.

这篇关于Tomcat 7 应用程序 Java 环境变量(无 JNDI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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