使用 Websphere 读取 Java 中的环境变量
我在使用 Websphere 应用程序服务器 7.0 (WAS7) 和环境变量的读取方面遇到了一些问题。
在 TomCat 中,我定义了一个变量,
<Environment name="myVar" type="java.lang.String" value="myVarOnServeur"
并通过在initialContext 上查找来读取它:
Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "java:comp/env/myVar" );
它有效!
但是对于 Websphere,我在 GUI 上定义了一个环境变量,但无法在 java 代码中读取它。我有一个命名异常。
(来源:fullahead.org)
我该如何解决我的问题?
I've a little problem with Websphere application server 7.0 (WAS7) and the reading of Environment Varaibles.
With TomCat, I've defined a variable as
<Environment name="myVar" type="java.lang.String" value="myVarOnServeur"
and I read it with a lookup on the initialContext :
Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "java:comp/env/myVar" );
and it works!
But with Websphere, I define a environment variable on the GUI but I can't read it in my java code. I've a NamingException.
(source: fullahead.org)
How can I do to fix my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在web.xml里面定义,
用java查看
to define inside web.xml
to see with java
你看错地方了。
您应该在环境 -> 命名 -> 命名空间绑定 -> 新建中添加变量。
如果您选择绑定类型 String、“绑定标识符”和“命名空间中的名称...”myVar,您可以通过以下方式获取变量的值:
You are looking at the wrong place.
You should add the variable in Environment->Naming->Name space bindings->New.
If you choose Binding type String, "Binding identifier" and "Name in namespace..." myVar, you can get variable's value with:
在 WAS 上遵循上述设置,其中 name 是您的键,value 是您的属性值。在我的示例中,我使用名称:测试值:这是测试值。设置此值后重新启动应用程序服务器。在您的 Java 代码中调用 System.getProperty("TEST"),其中 test 是您的属性的名称,该值将显示
On WAS follow the above setting where name is your key and value is your property value. in my example i used Name : Test Value : This is the test value. After setting this values restart your application server. on your Java code call System.getProperty("TEST") where test is the name for your property and the value will show
您可以在
web.xml
文件中放入类似以下内容,该文件应位于应用程序的WEB-INF
目录中:顺便说一下,这是标准语法,应该可以工作跨所有应用程序服务器。我将它与 WebSphere、JBoss 和 WebLogic 一起使用。可以像您在示例中一样进行查询。
You can put something like the following in your
web.xml
file, which should be in your application'sWEB-INF
directory:By the way this is a standard syntax and should work across all the application servers. I'm using it with WebSphere, JBoss and WebLogic. It can be queried exactly as you do in your example.
如果您想要定义和管理自己的变量,请查看环境->命名->命名空间绑定。您可以在那里将 jndi 名称绑定到字符串常量。请参阅 字符串绑定设置
If what you want is to define and manage your own variables, have a look at Environment->Naming->Name space bindings. You can bind jndi names to String constants there. see String binding settings
Websphere 7.0 - 8.5
设置变量
管理控制台---> Websphere 应用程序服务器----->您的服务器名称 ---> Java与进程管理--->进程定义-->Java虚拟机-->自定义属性
在 Java 中获取值
System.getProperty(“你的变量”)
Websphere 7.0 - 8.5
Set Variable
Admin Console ---> Websphere Application servers -----> Your_sever_name ---> Java and process management ---> Process definition -->Java Virtual Machine --> Custom properties
Get Value in Java
System.getProperty("Your_Variable")
您应该能够通过 WebSphere 的 AdminOperations 来解决这些问题 MBean:
请参阅 创建、编辑和删除 WebSphere 变量。
You should be able to resolve these via WebSphere's
AdminOperations
MBean:See Creating, editing and deleting WebSphere variables.
我只想详细说明如何在 WebSphere 中创建可供 Java 应用程序使用的变量,希望能够帮助其他人,因为我必须做一些额外的研究才能弄清楚这一点。
假设您要在 WebSphere 中创建一个名为 ENV 的变量,其中包含值 dev(或 int、或 prod,或任何其他值)。
服务器类型 > WebSphere 应用程序服务器。
在此示例中,创建了一个名为 ENV 且值为“dev”的变量。
接下来,需要将 Java 应用程序配置为使用 WebSphere 中的 ENV 变量。在下面的标记中,Java 应用程序有一个名为“Environment”的类。该类创建一个名为 env 的变量。 System.getProperty("ENV") 是从 WebSphere 获取变量的魔法。值得注意的是,此 Java 代码还应该与其他应用程序服务器一起使用,例如 JBoss 或 Tomcat,因此您不需要针对特定平台定制 Java 代码。
虽然绝对不是必需的,但我也会返回 env。我这样做只是为了演示,以便我们可以在JSP页面中获取变量,以便我们可以在JSP页面中亲眼看到变量,以验证这是否按预期工作。
在 JSP 页面的标记内部,我添加以下标记以从 Environment 类获取 env 变量,该类又从 WebSphere 获取 ENV 变量。
现在,一旦应用程序部署到 WebSphere,就应该显示环境,这就是我知道我能够成功从应用程序服务器获取变量的方式。
I would just like to elaborate on creating a variable in WebSphere that can be used by a Java app, to hopefully help others, as I had to do a bit of additional research to figure this out.
Let's say you want to create a variable in WebSphere named ENV which contains a value of dev (or int, or prod, or any other value).
Server Types > WebSphere application servers.
In this example, a variable named ENV with a vaule of "dev" was created.
Next, the Java app will need to be configured to use the ENV variable in WebSphere. In the below markup, the Java app has a class named "Environment". This class creates a variable named env. System.getProperty("ENV") is the magic that gets the variable from WebSphere. It is noteworthy that this Java code should also work with other application servers, such as JBoss or Tomcat, so you don't need to customize the Java code to a particular platform.
While definitely not required, I also am returning env. I am just doing this for demonstration, so that we can get the variable in a JSP page, so that we can see the variables with our own eyes in a JSP page, for validation that this works as expected.
Inside of the tags of a JSP page, I add the following markup to get the env variable from the Environment class, which in turn gets the ENV variable from WebSphere.
Now, once the app has been deployed to WebSphere, the environment should be displayed, which is how I know that I was able to successfully get the variable from the application server.
该线程有点旧,但只是想提供一些信息。这是在 WebSphere 8.5.5 中,
我尝试通过 [Environment > ] 在控制台中定义 WebSphere 变量。 WebSphere 变量] using
它没有给我变量。我在网上查了一下,发现了以下内容:
https://www.setgetweb.com/p/WAS855/ae/was2873。 html
下面列出的函数返回变量
然后调用
The thread is kind of old but just wanted to provide some info. This is with WebSphere 8.5.5
I tried getting WebSphere variables defined in the console via [Environment > WebSphere variables] using
It did not give the variable to me. I looked around a bit on the web and came across the following:
https://www.setgetweb.com/p/WAS855/ae/was2873.html
The following function listed there returns the variables
Then call
据IBM 官方文档报道, WebSphere 中的环境变量应按如下方式设置:
选择服务器 > (展开服务器类型)> WebSphere应用程序服务器>服务器名称> (展开 Java™ 和进程管理)>流程定义>环境条目> New
然后您可以像常规应用程序参数一样访问该变量。
Spring 用户可以使用 @Value 注解访问该变量:
As reported by the official IBM documentation, an environment variable in WebSphere should be set this way:
Select Servers > (Expand Server Types) > WebSphere application servers > server_name > (Expand Java™ and Process Management) > Process Definition > Environment Entries > New
Then you can access the variable like a regular application argument.
Spring users can access the variable with the @Value annotation:
我没有看到任何内容表明这些条目可以通过 ctx.lookup( "java:comp/env/..." ); 读取
I don't see anything there that says that those entries can be read via ctx.lookup( "java:comp/env/..." );