Java 如何在was liberty中获取全局属性,如应用程序名称

Java 如何在was liberty中获取全局属性,如应用程序名称,java,websphere-liberty,open-liberty,Java,Websphere Liberty,Open Liberty,我在谷歌上搜索,但在接下来的两行中,我发现很难找到传递给config.getServletContext().getAttribute()方法的字符串的translate。 这些行来自WAS 8.5上部署的应用程序 String applicationName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.enterprise.application.name"); String serv

我在谷歌上搜索,但在接下来的两行中,我发现很难找到传递给config.getServletContext().getAttribute()方法的字符串的translate。 这些行来自WAS 8.5上部署的应用程序

String applicationName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.enterprise.application.name");
String serverName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.application.host");
我现在在自由号上工作/学习

如果能链接到Liberty所有可能属性的参考(如果存在…),可能会更好

编辑

很抱歉,我以前无法测试它,因为应用程序中的其他错误阻止了执行,但事实证明,传递给getAttribute方法的字符串即使在我的本地Liberty测试服务器上也是有效的,因此:

String applicationName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.enterprise.application.name");
String serverName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.application.host");
在WebSphere8.5和Liberty上都有效

@Andy Guibert方法也可以检索AppName和主机名,即使是以更一般的方式

无论如何,我想更深入地讨论这个主题,并且我想找到一些文档,这些文档是我可以传递给(String)config.getServletContext().getAttribute(“”)的所有可能字符串,以便在IBM环境中检索信息,特别是Liberty(如果与经典Websphere有任何区别的话)


但是我找不到它…

在JavaEE应用程序中获取应用程序名称的一种简单/标准方法是使用以下内置JNDI名称:

import javax.naming.InitialContext;
// ...
字符串appName=InitialContext.doLookup(“java:app/appName”);
要获取主机名,您可能只需使用JavaSE API即可:

InetAddress.getLocalHost().getHostName()
您还可以在server.xml中定义和查找任意变量,然后使用MicroProfile config进行查找,如下所示:

@Inject
@ConfigProperty(name=“foo”,defaultValue=“bar”)
字符串属性;

另外,下面是Liberty的MicroporFile配置指南:

java没有全局变量的概念。不过,您可以创建公共静态最终变量。@Falco关于可以传递给
(字符串)config.getServletContext().getAttribute(“”)的值的后续问题,您可以通过调用
config.getServletContext().getAttributeNames()来检查自己