开发者

External properties file with Weblogic

开发者 https://www.devze.com 2023-03-19 11:08 出处:网络
I\'m looking for the best way to use an external properties file with an application that is going to be deployed on Weblogic 10.3 server. I read a number of articles on the site but I don\'t want to

I'm looking for the best way to use an external properties file with an application that is going to be deployed on Weblogic 10.3 server. I read a number of articles on the site but I don't want to hard-code the path to the properties file or put the file in the domains/mydomain folder.

Is there a dynamic way of doing 开发者_如何学编程this so when the application is deployed the properties file is also installed for example under the deployments folder and read from there?

Many thanks


Another alternative that does not require putting the file in a place other applications will read it is to use the Generic File Loading Overrides: http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/config.htm#i1066493

This involves creating a directory that will be the root directory of your deployment, let's call it FooApplication that has FooApplication.ear and FooWeb.war. This is called the Application Installation Directory. Your application goes in the FooApplication/app sub-directory whether it is an archive (like .ear, .war, jar) or whether it is an exploded version of one of those archives. Your optional deployment plan (you must have one to use this feature, it could be a plan that does not do much beyond specifying a config-root element and values as described in the documentation) goes in the FooApplication/plan. You can put your properties that you want to override ones in the application in FooApplication/plan/AppFileOverrides directory structure. http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/deployunits.htm#sthref9

Once that style of deployment is done, you write code like this from your application and the contents of myApp.properties get read from the FooApplication/plan/AppFileOverrides/FooWeb.war/myApp.properties will be the actual file that is read in.

Properties myAppProps = new Properties();
InputStream iostream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("myCfg/myApp.properties");
myAppProps.load(iostream);

This is accomplished by adding a classloader to your application as explained in the docs. It might seem tedious to initially configure, but it is a feature that directly satisfies the original question and only for that particular application.


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

  • Create a folder in %DOMAIN_HOME%\config\ folder. example appConfig.
  • Place your custom property file (Let's say config.properties) in appConfig directory/folder.
  • 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:

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

You can access that file in you java code as below:

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

Hope this helps.


Approach #2

Use Weblogic shared library

http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/libraries.html

Follow below steps

  • Package all your configuration as separate JAR during the build process
  • Deploy configuration JAR as shared library
  • Reference above shared library from your EAR/WAR
  • Deploy EAR/WAR (Configurations will be available in classpath)


When you say " I read a number of articles on the site but I don't want to hard-code the path to the properties file" I assume you are saying you don't want to hard code it in your Java code. If that is so, then please see below

Answered here:

There are ways to read properties file in Java from weblogic classpath

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.

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.

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

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

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

Hope it helps!


Approach #1

Update your server startup script to pass below system variable to JVM (below is example on Windows OS)

call "%DOMAIN_HOME%\bin\startWebLogic.cmd" "-Dcom.mycompany.myapp.EXTERNAL_CONFIG_PATH=/mycompany/myapp/config" %*

Using this variable which points to your configuration directories, read configurations from there. You will need to make this setting on each server where you want to deploy your application.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号