简体   繁体   English

在weblogic中使用外部属性文件

[英]Using external properties files in weblogic

I am working on deploying a J2ee application that I have previously been deploying in JBOSS into Weblogic 10.3.1.0. 我正在将我之前在JBOSS中部署的J2ee应用程序部署到Weblogic 10.3.1.0中。 I am running into an issue with external properties files. 我遇到了外部属性文件的问题。 In Jboss I can just put the properties files into $JBOSS_HOME/server/default/conf, and they are loaded onto the system classpath and I can access them without any problems. 在Jboss中我可以将属性文件放入$ JBOSS_HOME / server / default / conf中,然后将它们加载到系统类路径中,我可以毫无问题地访问它们。 I was able to put shared libraries into $MIDDLEWAREHOME/user_projects/domains/mydomain/lib and they were loaded into the system classpath without any problems but I am unable to load properties files. 我能够将共享库放入$ MIDDLEWAREHOME / user_projects / domains / mydomain / lib中,并将它们加载到系统类路径中,没有任何问题,但我无法加载属性文件。

Does anyone know how to include external properties files in Weblogic? 有谁知道如何在Weblogic中包含外部属性文件?

Thanks, 谢谢,

I figured this out and have it working the way I would expect. 我想出了这个,让它以我期望的方式运作。 First I did try the suggestions as above. 首先,我尝试了上述建议。 If i added a folder to my classpath, or put the properties files in a folder on my classpath, the jars in the file were picked up, but not properties files. 如果我将一个文件夹添加到我的类路径中,或者将属性文件放在我的类路径上的文件夹中,则会拾取文件中的jar,但不会拾取属性文件。 If i put my properties files in a jar, and put them in a folder on my classpath everything worked. 如果我将我的属性文件放在一个jar中,并将它们放在我的类路径上的一个文件夹中,一切正常。 But I did not want to have jar my files everytime a change was made. 但是每次做出改变时我都不想让我的文件变成jar。 The following works in my env. 以下是我的环境中的作品。

If i place the properties files in %WEBLOGIC_HOME%/user_projects/domains/MYDOMAIN then they are getting picked up, without having to be placed in a jar file. 如果我将属性文件放在%WEBLOGIC_HOME%/ user_projects / domains / MYDOMAIN中,那么它们将被拾取,而不必放在jar文件中。

在weblogic中,jar将从lib加载,非jar文件将从域文件夹中加载

There are ways to read properties file in Java from weblogic classpath 有一些方法可以从weblogic类路径中读取Java中的属性文件

One (Properties file located in the weblogic domain): Drop the properties file inside the Domain directory. 一个 (位于weblogic域中的属性文件):删除域目录中的属性文件。 This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream. 这样,属性文件就会自动添加到weblogic类路径中,我们可以使用resourceAsStream从Java读取。

Two (Properties file from a User defined location):The advantage with this approach is that the property file can reside outside the JAR or EAR file and can be modified conveniently. 两个 (来自用户定义位置的属性文件):此方法的优点是属性文件可以驻留在JAR或EAR文件之外,并且可以方便地进行修改。

package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyFileExample {

private static Properties prop;

public static void myMethod() {

  InputStream is = null;

  try {

    prop = new Properties();

    String propFilePath = System.getProperty(“propFileLocation“);

    InputStream iStream =     PropertyFileExample.class.getClassLoader().getResourceAsStream(propFilePath);

    //Note that the propFilePath is a -Dparam defined below in the setDomainEnv
    prop.load(iStream);
    prop.getProperty(“dbuser”);

  } catch (FileNotFoundException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }
}
}

In the weblogic setDomainEnv (under bin) => we need to pass the location of the property file as a -D argument to JAVA_OPTIONS 在weblogic setDomainEnv(在bin下)=>我们需要将属性文件的位置作为-D argument传递给JAVA_OPTIONS

set JAVA_OPTIONS=%JAVA_OPTIONS% -DpropFileLocation =/dev/file/properties/some.properties

You can set a directory on the classpath and Place your custom properties file in that folder/directory. 您可以在类路径上设置目录,并将自定义属性文件放在该文件夹/目录中。 So that, the entire directory along with property file will be on classpath. 这样,整个目录和属性文件将在classpath上。 To set the directory on the classpath in weblogic 10.3.x 在weblogic 10.3.x中的类路径上设置目录

  • Create a folder in %DOMAIN_HOME%\\config\\ folder. %DOMAIN_HOME%\\config\\文件夹中创建一个文件夹。 example appConfig . 示例appConfig
  • Place your custom property file (Let's say config.properties ) in appConfig directory/folder. 将您的自定义属性文件(比如说config.properties )放在appConfig目录/文件夹中。
  • Modify the setDomainEnv.cmd (Windows) to include appConfig in the classpath by setting %DOMAIN_HOME%\\config\\appConfig as value to EXT_POST_CLASSPATH (this variable is already defined in the setDomainEnv.cmd file) variable as below: 通过将%DOMAIN_HOME%\\config\\appConfigEXT_POST_CLASSPATH (此变量已在setDomainEnv.cmd文件中定义)变量,修改setDomainEnv.cmd (Windows)以在类路径中包含appConfig ,如下所示:

     set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\\config\\appConfig 

You can access that file in you java code as below: 您可以在java代码中访问该文件,如下所示:

    InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream ("config.properties");
    Properties prop = new Properties();
    prop.load(inputStream);
    String value = prop.getProperty("key");

Hope this helps. 希望这可以帮助。

虽然这可能需要额外的努力,但如果将文件放入JAR中,然后将它们放入lib目录中,那应该可行。

You can look at your setDomainEnv.cmd (Windows) or setDomainEnv.sh (Unix/Linux) script in your domain files and see what locations are added in the CLASSPATH for your domain. 您可以在域文件中查看setDomainEnv.cmd (Windows)或setDomainEnv.sh (Unix / Linux)脚本,并查看在域的CLASSPATH中添加的位置。 Then just choose one folder and place the properties file there, if you want a specific location for your properties file just edit the script. 然后只需选择一个文件夹并将属性文件放在那里,如果您想要属性文件的特定位置,只需编辑脚本即可。

The most flexible way is to use weblogic deployment plans and Generic File Loading overrides 最灵活的方法是使用weblogic部署计划和通用文件加载覆盖

External properties file with Weblogic Weblogic的外部属性文件

http://docs.oracle.com/cd/E21764_01/web.1111/e13702/config.htm#DEPGD188 http://docs.oracle.com/cd/E21764_01/web.1111/e13702/config.htm#DEPGD188

that was my solution: 这是我的解决方案:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        InputStream is = null;

        String urlExte = System.getenv("DOMAIN_HOME")+"/properties/SmsBalanceadoWS/";


        org.springframework.core.io.Resource resource = ctx.getResource( "file:"+urlExte+"/application.properties");
        try {
            is = resource.getInputStream();
        } catch (IOException e) {
            LOGGER.debug("ERROR"+ e.getMessage());
        }

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

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