JavaBlog.fr / Java.lu DEVELOPMENT,Java,Spring Java: Use of JVM variable argument (Spring, properties file, outside of classpath scope)

Java: Use of JVM variable argument (Spring, properties file, outside of classpath scope)

Hello,

I know, I have not been writing on my blog since a long time, but, I have been very busy with a challenge project. So, today, I start with a mini-post concerning the use of JVM variable/argument. We will study a concret example to specifiy the place of a configuration file outside of classpath scope.

Needs
We need to precise the place of a configuration file without hard code its path in the application.
Our properties file is MyConfigFile.properties with the simple content:

1# ########## Comment ########
2my.config.property123=val123
3my.config.property465=val465

… but, how specify the path of this file in Spring XML configuration file (placeholder)? in Java code?

Set the JVM argument
The solution is use of JVM argument, which doesn’t need to modify the application deliveries.
The setting of JVM argument/variable is done during the starting of AS, below the example in TOMCAT server configuration edition:

1-Dmyconfigfile="file:/projects/config/MyConfigFile.properties"

…in this example, we have create a variable myconfigfile to specify the path to a configuration file named MyConfigFile.properties.

Note:
The “file:/” notation precise that it is a path to file. For example file:/C:/projects/config/MyConfigFile.properties, file:/C:\\projects\\config\\MyConfigFile.properties are possible. But, with a value of variable like file:/projects/config/MyConfigFile.properties the properties file must be on the same disk drive than the JRE installation folder. For example, if your used JRE is installed in C:\Program Files (x86)\Java\jre6, then, your properties MyConfigFile.properties file must be in C:\projects\config\.

Use of JVM variable in the Spring
The use of JVM variable in the Spring XML configuration file is very simple:

1${myconfigfile}

…like this example of PropertyPlaceholderConfigurer:

01<?xml version="1.0" encoding="UTF-8"?>
03...>
04    <!--
05    <bean id="propertyConfigurer"
06        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
07        <property name="locations">
08            <list>
09                <value>classpath:MyConfigFile.properties</value>
10            </list>
11        </property>
12    </bean>
13    -->
14 
15    <!--
16    Configuration properties are outside of classpath scope
17    It could be set through Tomcat's environment variable<
18    -Dmyconfigfile="file:/projects/config/MyConfigFile.properties"
19    -->
20    <context:property-placeholder location="${myconfigfile}">
21</beans>

Use of JVM variable in Java
The use of JVM variable in the Java code is very simple:

1String configFile = System.getProperty("myconfigfile");

…like in the following example:

01//...
02 
03// Use of an environment variable to set file location; use the same var for Spring config name
04// = "file:/projects/config/MyConfigFile.properties"
05String configFile = System.getProperty("myconfigfile");
06 
07 
08// Use of ClassLoader to get the stream of a file in classpath
09/*
10Properties result = null;
11ClassLoader  loader = this.getClassLoader();
12if (loader == null){
13       loader = ClassLoader.getSystemClassLoader ();
14}
15if (loader != null){
16       in = loader.getResourceAsStream (configFile);
17}
18*/
19 
20 
21// Use of resource loader of Spring to get the stream of a file which is outside of classpath scope
22{
23       ResourceLoader resourceLoader = new DefaultResourceLoader();
24       Resource resource = resourceLoader.getResource(configFile);
25       if(resource != null){
26              in = resource.getInputStream();             
27       }
28}
29 
30 
31 
32// Load the properties file
33Properties result = new Properties ();
34if (in != null){
35       // version 1
36//       result.load(in); // Can throw IOException
37        
38       // version 2
39       DefaultPropertiesPersister propPersist = new DefaultresourceLoader();
40       propPersist.load(result, new InputStreamReader(resource.getInputStream());
41}
42 
43 
44 
45 
46// Get a property from configuration file
47{
48       Object value = result.get("my.config.property123");
49       Boolean val = null;
50       //...
51}
52 
53 
54//...

That’s all!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post