Read variable by name from server.xml

bobbyrne01 :
<?xml version="1.0" encoding="UTF-8"?>
<server>

  <variable 
    name="temp" 
    value="Something" />
</server>

How can I programmatically read temp from the xml file from a Java based web application?

Was trying:

String temp = System.getenv("temp");
Andy Guibert :

There are a few options you have to do this:

1. Using MicroProfile Config

In server.xml:

<featureManager>
  <feature>mpConfig-1.3</feature>
  ....
</featureManager>

<variable  name="temp"  value="Something" />

Then inject into any CDI bean:

@Inject
@ConfigProperty(name = "temp")
String temp;

2. Store the variable in JNDI with <jndiEntry>

In server.xml:

<featureManager>
  <feature>jndi-1.0</feature>
  ....
</featureManager>

<jndiEntry jndiName="config/foo" value="whatever"/>
<jndiEntry jndiName="config/bar" value="${temp}"/>
<jndiEntry jndiName="config/configDir" value="${server.config.dir}"/>

With resource injection in a servlet or EJB (or other managed class):

@Resource(lookup = "config/foo")
String foo;

Or programmatic JNDI lookup:

String configDir = InitialContext.doLookup("config/configDir");

3. Using environment variables:

This approach requires you to set environment variables in ${server.config.dir}/server.env, or in the environment of the process that starts the Liberty server. Everything in here will end up in the server JVM's env.

temp=Something
foo=bar

Then get as env var in an application:

String temp = System.getenv("temp");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130333&siteId=1