如何在 EAR 或 WAR 之外存储 Java EE 配置参数? [英] How can I store Java EE configuration parameters outside of an EAR or WAR?

查看:20
本文介绍了如何在 EAR 或 WAR 之外存储 Java EE 配置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 web 项目(ear/war 文件)之外存储 web 项目的配置.应用程序不应该知道它在哪个容器中运行(WebSphere/JBoss 等).

I want to store configuration for a web project outside of the web project (ear/war file). The application shouldn't know in which container it's running (WebSphere/JBoss etc.).

处理这个问题的最佳方法是什么?

What is the best way to handle this?

JNDI 是一种干净的方式吗?如果 JNDI 可以解决我的问题,我应该如何配置它?(自定义对象?)

Is JNDI a clean way? If JNDI can solve my problems, how should I configure it? (Custom Objects?)

就我而言,SOAP/WS 端点只有简单的 Key=>Value 对 (String,String).

推荐答案

见这个 问题 用于读取 WAR 文件之外的属性文件.

See this question for reading properties file outside of the WAR file.

有关读取变量值的信息,请参阅此问题来自 JNDI.我相信这是最好的解决方案.您可以使用以下代码读取字符串变量:

See this question for reading variable values from JNDI. I believe that this is the best solution. You can read a String variable with this code:

Context initialContext = new InitialContext();
String myvar = (String) initialContext.lookup("java:comp/env/myvar");

以上代码适用于所有容器.在 Tomcat 中,您在 conf/server.xml 中声明以下内容:

The above code will work on all containers. In Tomcat you declare the following in conf/server.xml:

<GlobalNamingResources ...>
  <Environment name="myvar" value="..."
         type="java.lang.String" override="false"/>
</GlobalNamingResources>

以上将创建一个全局资源.也可以在应用程序的上下文中定义资源.在大多数容器中,JNDI 资源可通过 MBean 管理控制台获得.其中一些提供了图形界面来编辑它们.进行更改时,最多需要重新启动应用程序.

The above will create a global resource. It is also possible to define a resource in the context of application. In most containers the JNDI resources are available through a MBeans Management Console. Some of them offer a graphical interface to edit them. At most an application restart is needed, when a change is made.

如何定义和编辑 JNDI 资源是特定于容器的.应用适当的设置是配置人员/管理员的工作.

How JNDI resources are defined and edited is container specific. It is the job of the configurator/administrator to apply the appropriate settings.

这些是 JNDI 提供的好处:

These are the benefits offered by JNDI:

  • 您可以在 WAR/EAR 文件中定义参数的默认值.
  • 可以在容器中轻松配置参数.
  • 修改参数值时无需重启容器.

这篇关于如何在 EAR 或 WAR 之外存储 Java EE 配置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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