Java 在 weblogic 中使用外部属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1245955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:24:28  来源:igfitidea点击:

Using external properties files in weblogic

javajakarta-eepropertiesweblogicclasspath

提问by broschb

I am working on deploying a J2ee application that I have previously been deploying in JBOSS into 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. 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.

我正在将之前在 JBOSS 中部署的 J2ee 应用程序部署到 Weblogic 10.3.1.0 中。我遇到了外部属性文件的问题。在 Jboss 中,我可以将属性文件放入 $JBOSS_HOME/server/default/conf,并将它们加载到系统类路径中,我可以毫无问题地访问它们。我能够将共享库放入 $MIDDLEWAREHOME/user_projects/domains/mydomain/lib 并且它们被加载到系统类路径中而没有任何问题,但我无法加载属性文件。

Does anyone know how to include external properties files in Weblogic?

有谁知道如何在 Weblogic 中包含外部属性文件?

Thanks,

谢谢,

采纳答案by broschb

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. If i put my properties files in a jar, and put them in a folder on my classpath everything worked. But I did not want to have jar my files everytime a change was made. The following works in my env.

我想出了这一点,并让它按照我期望的方式工作。首先,我确实尝试了上述建议。如果我在我的类路径中添加了一个文件夹,或者将属性文件放在我的类路径上的一个文件夹中,那么文件中的 jars 会被选中,但不会被选中。如果我将我的属性文件放在一个 jar 中,并将它们放在我的类路径上的一个文件夹中,一切正常。但是我不想在每次更改时都让我的文件 jar。以下在我的环境中有效。

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 文件中。

回答by Adam Batkin

Although it may be a little extra effort, if you put the files into a JAR before dropping them into that libdirectory, that should work.

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

回答by victor hugo

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. 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.

您可以查看域文件中的setDomainEnv.cmd(Windows) 或setDomainEnv.sh(Unix/Linux) 脚本,并查看在域的 CLASSPATH 中添加了哪些位置。然后只需选择一个文件夹并将属性文件放在那里,如果您想要属性文件的特定位置,只需编辑脚本。

回答by suresh

In weblogic jars will be loaded from the lib and the non jar files will be loaded from the domain folder

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

回答by Rahul Thotakura

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. This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream.

(位于 weblogic 域中的属性文件):将属性文件放入域目录中。通过这种方式,属性文件会自动添加到 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 argumentto JAVA_OPTIONS

在weblogic的setDomainEnv(bin下)=>我们需要将属性文件的位置作为a-D argument传递给JAVA_OPTIONS

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

回答by net user

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. To set the directory on the classpath in weblogic 10.3.x

您可以在类路径上设置一个目录,并将您的自定义属性文件放在该文件夹/目录中。这样,整个目录以及属性文件都将位于类路径上。在 weblogic 10.3.x 中设置类路径上的目录

  • Create a folder in %DOMAIN_HOME%\config\folder. example appConfig.
  • Place your custom property file (Let's say config.properties) in appConfigdirectory/folder.
  • Modify the setDomainEnv.cmd(Windows) to include appConfigin the classpath by setting %DOMAIN_HOME%\config\appConfigas value to EXT_POST_CLASSPATH(this variable is already defined in the setDomainEnv.cmdfile) variable as below:

    set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
    
  • %DOMAIN_HOME%\config\文件夹中创建一个文件夹。例子appConfig
  • 将您的自定义属性文件(假设config.properties)放在appConfig目录/文件夹中。
  • 通过将值设置为(此变量已在文件中定义)变量setDomainEnv.cmd来修改(Windows)以包含appConfig在类路径中,如下所示:%DOMAIN_HOME%\config\appConfigEXT_POST_CLASSPATHsetDomainEnv.cmd

    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.

希望这可以帮助。

回答by Kalpesh Soni

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

回答by user2522715

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());
        }