Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

tomcat7 - Tomcat 7 application Java environment variables (JNDI-less)

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

<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">

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 {
}

It is then possible to use the @ProductProject annotation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...