简体   繁体   English

如何在WebSphere中读取外部属性文件?

[英]How to read external properties files in WebSphere?

I developed a sample web application which will read the data from an external properties file. 我开发了一个示例Web应用程序,它将从外部属性文件中读取数据。 The properties file is in the source folder in my system and is not included inside the WAR file. 属性文件位于系统的源文件夹中,不包含在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"));
  1. How do I access this property file externally inside the WAR file? 如何在WAR文件外部访问此属性文件?
  2. What changes have to be made in the code to read it in the WAR file? 在WAR文件中读取代码需要进行哪些更改?

I think the most versatile approach is to define a simple environment entry as described in the section EE.5.4 Simple Environment Entries of Java™ Platform, Enterprise Edition (Java EE) Specification, v5 . 我认为最通用的方法是定义一个简单的环境条目,如Java™平台,企业版(Java EE)规范v5的 EE.5.4简单环境条目一节所述。

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 Types of the specification. 您还可以使用URL连接工厂,如规范EE.5.6.1.4标准资源管理器连接工厂类型部分所述。

The Application Component Provider must use the java.net.URL resource manager connection factory type for obtaining URL connections. Application Component Provider必须使用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应用程序的部署描述符WEB-INF/web.xml中定义资源引用,以便您可以使用@Resource注入值或使用JNDI API和java: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的要求,您可以使用propertiesPathpropertiesURL ,就好像它们作为输入参数传递给您的方法一样。

Now, it's time to meet expectations of WebSphere Application Server. 现在,是时候满足WebSphere Application Server的期望了。

What you defined are logical names that need to be mapped to their administered names (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中,您使用具有以下配置的WebSphere Binding描述符WEB-INF/ibm-web-bnd.xml

<?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. 部署应用程序时,WAS允许您将这些映射映射到其管理的资源。 Use the ISC console to define values of the environment entries and map them to the application. 使用ISC控制台定义环境条目的值并将它们映射到应用程序。

It has became easier with WebSphere Liberty Profile. 使用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 . 我在我的文章中使用@Resource来访问WebSphere AS 8.5 Liberty Profile中的JNDI,描述了WLP提供的机制。

You have three options: 你有三个选择:

  1. configure the Websphere to include the directory which contains the property file in the classpath. 配置Websphere以包含类路径中包含属性文件的目录。 Don't know how to do it, but I'm sure it is possible, since our application does the same thing 不知道怎么做,但我确信它是可能的,因为我们的应用程序做同样的事情

  2. include the property file in the war archive. 在war存档中包含属性文件。 You probably don't want to do that. 你可能不想这样做。

  3. instead using the classloader to load the property file use the file api with an absolute path. 而是使用类加载器加载属性文件,使用带有绝对路径的文件api。 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. 我不完全确定WAS是否允许这样做,但无论如何这都是一个坏主意,因为它使您的应用程序非常依赖于它真正不应该关心的事情,例如应用程序的安装路径。

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM