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:
# ########## Comment ######## my.config.property123=val123 my.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:
-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:
${myconfigfile}
…like this example of PropertyPlaceholderConfigurer:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ...> <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:MyConfigFile.properties</value> </list> </property> </bean> --> <!-- Configuration properties are outside of classpath scope It could be set through Tomcat's environment variable< -Dmyconfigfile="file:/projects/config/MyConfigFile.properties" --> <context:property-placeholder location="${myconfigfile}"> </beans>
Use of JVM variable in Java
The use of JVM variable in the Java code is very simple:
String configFile = System.getProperty("myconfigfile");
…like in the following example:
//... // Use of an environment variable to set file location; use the same var for Spring config name // = "file:/projects/config/MyConfigFile.properties" String configFile = System.getProperty("myconfigfile"); // Use of ClassLoader to get the stream of a file in classpath /* Properties result = null; ClassLoader loader = this.getClassLoader(); if (loader == null){ loader = ClassLoader.getSystemClassLoader (); } if (loader != null){ in = loader.getResourceAsStream (configFile); } */ // Use of resource loader of Spring to get the stream of a file which is outside of classpath scope { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource(configFile); if(resource != null){ in = resource.getInputStream(); } } // Load the properties file Properties result = new Properties (); if (in != null){ // version 1 // result.load(in); // Can throw IOException // version 2 DefaultPropertiesPersister propPersist = new DefaultresourceLoader(); propPersist.load(result, new InputStreamReader(resource.getInputStream()); } // Get a property from configuration file { Object value = result.get("my.config.property123"); Boolean val = null; //... } //...
That’s all!!!
Huseyin OZVEREN