How to load a properties?
Simply add the properties’ folder into the classpath, then load it in this way.
private static Properties prop = new Properties();
prop.load(Constants.class.getClassLoader().getResourceAsStream("application.properties"));
Class.getResourceAsStream() & ClassLoader的getResourceAsStream()
注意两点:
1,用Class.getResourceAsStream() 时,路径应该是以"/"开头的,如:
mypackage.Hello.class.getResourceAsStream("/config/config.ini");
2,如果直接用ClassLoader的getResourceAsStream() 不用以"/"开头.如,
mypackage.Hello.class.getResourceAsStream("config/config.ini");
All kinds of methods to get path
package com.zcjl.test.base;
import java.io.File;
public class Test
{
public static void main(String[] args) throws Exception
{
System.out.println(
Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println(Test.class.getClassLoader().getResource(""));
System.out.println(ClassLoader.getSystemResource(""));
System.out.println(Test.class.getResource(""));
System.out.println(Test.class.getResource("/"));
System.out.println(new File("").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
}
}
file:/E:/workSpace/javaTest/target/classes/
file:/E:/workSpace/javaTest/target/classes/
file:/E:/workSpace/javaTest/target/classes/
file:/E:/workSpace/javaTest/target/classes/javaAPI/
file:/E:/workSpace/javaTest/target/classes/
E:\workSpace\javaTest
E:\workSpace\javaTest
. and .. are evil
That’s because . always means the current folder, and .. always means the parent folder. This is a dirty evil solution. Though it looks like it works fine now, but you really don’t know when it will break.
That’s because you will never know how and where your program will be invoked.
For example, your code make be called from an ant build.xml file, from a direct unit test java program, from a batch file. And these invoker entities have different locations. As these calling locations change, the value of . and .. also change.
The solution is, use absolute location.
The solution is, use system properties.
When you start your application, always pass in a project_root system properties.
As long as you have this system properties configged, you can config your log4j.xml like this:
<appender name="quantumlogger" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${project_root}/log/Quantum.log"/>
<param name="Append" value="true"/>
<param name="Threshold" value="DEBUG"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'dd-MM-yyyy"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] (%t:%x) %m%n"/> <!-- This pattern incurs less performance penalty -->
</layout>
</appender>
As long as your have this system properties configged, you can write your UnitTestBase.java like this:
public class UnitTestBase
{
protected static String project_root = null;
static
{
if(System.getProperty("project_root") == null)
{
System.setProperty("project_root", "C:/VIPER-DEV/eqtg/dev/hk/trading/BookingEngine");
project_root = System.getProperty("project_root");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("system property -- project_root can't be retrieved!!!!!!!!!!!!!");
System.out.println("Will use default value: " + project_root);
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
else
{
project_root = System.getProperty("project_root");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("system property -- project_root is successfully retrieved: " + project_root);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
}
}
How to setup a system properties?
1) Simply use –Dproject_root=somewhere in your java command, in your bat file or somewhere.
2) Simply use <sysproperty key="project_root" value="${bookingEngine.root}"/> in your build.xml ant file.
3) There are different ways of setting up a system property based on the context. And this is a popular requirement, simply google it. You will find the answer.