Read variable by name from server.xml

问题: <?xml version="1.0" encoding="UTF-8"?> <server> <variable name="temp" value="Something" /> </server> How can I programmatically read...

问题:

<?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");

回答1:

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");
  • 发表于 2019-03-23 04:41
  • 阅读 ( 154 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除