Results 1 to 4 of 4

Thread: Why do not use .java instead of .xml as config file?

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    1

    Default Why do not use .java instead of .xml as config file?

    Why do not use .java instead of .xml as config file?
    For example:
    My config file is C:\\T.java
    In the static block of class T we can our stuff.

    Use com.sun.tools.javac.Main to compile T.java and load the class
    when the container starts.


    Code:
    	/* 
    	 *compile the config file T.java 
    	 */
    	String dirStr = "C:\\";
    	String srcFileName = "T.java";
    	File dir = new File(dirStr);
    	String[] javacArgs = new String[] {
    				"-d", 
    				dirStr,
    				new File(dir, srcFileName).getAbsolutePath()
    	};
    	System.out.println("Compiling ...");
    	Main.compile(javacArgs);
    		
    	/* 
    	 *load the class and the static block of class T will be executed
    	 */	
    	URL url = new URL("file", null, dirStr+"/");
    	URL[] arr = new URL[]{url};
    	URLClassLoader loader = new URLClassLoader(arr, Thread.currentThread().getContextClassLoader());
    		
    	Class.forName("T", true, loader);

    config file T.java instead .xml

    Code:
    public class T {
    	static {
    	 	 /* 
    	 	  * do some configuration
    	 	  */
    		System.out.println("OOOKKK");	
    	}
    }

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    That is a possibility. However, I have, again a compilation step which is not there if I use an XML configuration (or another supported format).
    If, for some reason, I want to do configuration programmatically in java code I can do that without an additional compilation step, so I do not see an advantage here.

    Just my opinion,
    Andreas

  3. #3
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    If you want to avoid XML (for whatever reason, let's not go there. ), one of the dynamic script languages Spring supports might be a better idea than Java itself.
    --Jing Xue

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    There seems to be some work in-progress to provide Java Configuration option for Spring.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •