2009-10-20

Loading P6Spy Configuration File at Custom Position

P6Spy automatically loads configuration file named spy.properties on one of the top directories in classpath at initialization. For example, if your classpath is '/foo:/foo/bar.jar', /foo/spy.properties would be automatically configured.

But if you should use different name or position, for example /foo/spy-test.properties or /foo/baz/qux/spy.properties, you can use spy.properties system property to set the position and name of property file relative to classpath.
For example, if you want to load /foo/baz/qux/spy-sql.properties file with classpath of '/foo:/foo/bar.jar'., you can use command-line like the following. Note that the value should be relative to classpath. It is not /foo/baz/qux/spy-sql.properties but baz/qux/spy-sql.properties.

java -Dspy.properties=baz/qux/spy-sql.properties MyApplication.java

You can also specify the property file with custom position and name using p6.home system property. Refer the 'Command Line Options' section of the official document(http://www.p6spy.com/documentation/other.htm#common). But in this case, you should specify the absolute path of the property file, which is less portable.

In case of JUnit 4.x test case, you can set the system property in BeforeClass method which would be executed before loading P6Spy.

public class MyTest{
   @BeforeClass
   public static void setUpBeforeClass() throws Exception{
      System.setProperty("spy.properties", "baz/qux/spy-sql.properties");
   }

   @Test
   public void testMyMethod(){
      //...
   }

   //...
}

There are few interesting article about integrating Spring framework and P6Spy.

The configuration of P6Spy could be more flexible using above tip. When I have finished the testing, I will write it also.

0 comments:

Post a Comment