java 如何读取 WebSphere 中的外部属性文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15716154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read external properties files in WebSphere?
提问by user2210071
I developed a sample web application which will read the data from an external properties file. The properties file is in the source folder in my system and is not included inside the WAR file.
我开发了一个示例 Web 应用程序,它将从外部属性文件中读取数据。属性文件位于我系统的源文件夹中,不包含在 WAR 文件中。
The property file is accessed like this:
属性文件的访问方式如下:
Properties prop = new Properties();
//File f1 = new File("Property.properties");
prop.load(getClass().getClassLoader().getResourceAsStream("Property.properties"));
- How do I access this property file externally inside the WAR file?
- What changes have to be made in the code to read it in the WAR file?
- 如何在 WAR 文件中从外部访问此属性文件?
- 必须在代码中进行哪些更改才能在 WAR 文件中读取它?
回答by Jacek Laskowski
I think the most versatile approach is to define a simple environment entry as described in the section EE.5.4 Simple Environment Entriesof Java? Platform, Enterprise Edition (Java EE) Specification, v5.
我认为最通用的方法是在一节所述定义一个简单的环境条目EE.5.4简单的环境条目的Java的?平台,企业版 (Java EE) 规范,v5。
From the section (page 68):
从部分(第 68 页):
A simple environment entry is a configuration parameter used to customize an application component's business logic. The environment entry values may be one of the following Java types: String, Character, Byte, Short, Integer, Long, Boolean, Double, and Float.
一个简单的环境条目是一个配置参数,用于自定义应用程序组件的业务逻辑。环境条目值可以是以下 Java 类型之一:String、Character、Byte、Short、Integer、Long、Boolean、Double 和 Float。
You may also use URL connection factory as described in the section EE.5.6.1.4 Standard Resource Manager Connection Factory Typesof the specification.
您还可以使用规范的EE.5.6.1.4 标准资源管理器连接工厂类型部分中描述的 URL 连接工厂。
The Application Component Provider must use the java.net.URL resource manager connection factory type for obtaining URL connections.
应用程序组件提供程序必须使用 java.net.URL 资源管理器连接工厂类型来获取 URL 连接。
Both require a definition of a resource reference in the deployment descriptor WEB-INF/web.xml
of your web application so you can inject the value using @Resource
or use JNDI API with java:comp/env
as the entry point.
两者都需要在WEB-INF/web.xml
Web 应用程序的部署描述符中定义资源引用,以便您可以使用@Resource
或使用 JNDI APIjava:comp/env
作为入口点注入值。
The benefit is that you can change the configuration of your web application without having to recompile the code as well as let you change it using an application server's administrative tools your admins are accustomed with.
好处是您可以更改 Web 应用程序的配置,而无需重新编译代码,并允许您使用管理员习惯的应用程序服务器的管理工具来更改它。
In web.xml
you define the resource reference.
在web.xml
您定义资源引用中。
<resource-ref>
<res-ref-name>propertiesURL</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>propertiesPath</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Then in your code you use the following to access the values:
然后在您的代码中使用以下内容访问值:
@Resource
String propertiesPath;
@Resource
URL propertiesURL;
With this you met the requirements of Java EE and you can use propertiesPath
or propertiesURL
as if they were passed as input parameters to your methods.
这样您就满足了 Java EE 的要求,您可以使用propertiesPath
或propertiesURL
就好像它们作为输入参数传递给您的方法一样。
Now, it's time to meet expectations of WebSphere Application Server.
现在,是时候满足对 WebSphere Application Server 的期望了。
What you defined are logicalnames that need to be mapped to their administerednames (an application server knows about and can provide to the application).
您定义的是需要映射到其管理名称(应用程序服务器知道并可以提供给应用程序)的逻辑名称。
In WebSphere Application Server you use WebSphere Binding descriptor WEB-INF/ibm-web-bnd.xml
with the following configuration:
在 WebSphere Application Server 中,您使用WEB-INF/ibm-web-bnd.xml
具有以下配置的WebSphere 绑定描述符:
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_1.xsd"
version="1.1">
<virtual-host name="default_host" />
<resource-ref name="propertyURL" binding-name="propertyURL" />
<resource-ref name="propertyURL" binding-name="propertyURL" />
</web-bnd>
When the application gets deployed WAS allows you to map these mappings to its administered resources. Use the ISC console to define values of the environment entries and map them to the application.
部署应用程序后,WAS 允许您将这些映射映射到其管理的资源。使用 ISC 控制台定义环境条目的值并将它们映射到应用程序。
It has became easier with WebSphere Liberty Profile. I described the mechanism as offered by WLP in my article Using @Resource to access JNDI in WebSphere AS 8.5 Liberty Profile.
有了 WebSphere Liberty Profile,它变得更容易了。我在我的文章Using @Resource to access JNDI in WebSphere AS 8.5 Liberty Profile 中描述了 WLP 提供的机制。
回答by Jens Schauder
You have three options:
您有三个选择:
configure the Websphere to include the directory which contains the property file in the classpath. Don't know how to do it, but I'm sure it is possible, since our application does the same thing
include the property file in the war archive. You probably don't want to do that.
instead using the classloader to load the property file use the file api with an absolute path. I'm not completely sure WAS does allow that, but it is a bad idea anyway, because it makes your application very dependent on things that it really shouldn't care about, such as the installation path of your application.
配置 Websphere 以在类路径中包含包含属性文件的目录。不知道怎么做,但我相信这是可能的,因为我们的应用程序做同样的事情
在 war 档案中包含属性文件。你可能不想这样做。
而不是使用类加载器来加载属性文件,而是使用带有绝对路径的文件 api。我不完全确定 WAS 是否允许这样做,但无论如何这是一个坏主意,因为它使您的应用程序非常依赖它真正不应该关心的事情,例如应用程序的安装路径。
回答by weberjn
WebSphere has two folders on the classpath, properties can be loaded from there:
WebSphere 在类路径上有两个文件夹,可以从那里加载属性:
Enterprise Applications > myear > Manage Modules > myjar.jar > Class loader viewer 4 - Extension - com.ibm.ws.bootstrap.ExtClassLoader
企业应用程序 > myear > 管理模块 > myjar.jar > 类加载器查看器 4 - 扩展 - com.ibm.ws.bootstrap.ExtClassLoader
file:/projekte/IBM/WebSphere/AppServer-8.5/classes/
文件:/projekte/IBM/WebSphere/AppServer-8.5/classes/
file:/projekte/IBM/WebSphere/AppServer-8.5/lib/
文件:/projekte/IBM/WebSphere/AppServer-8.5/lib/