Announcement Announcement Module
Collapse
No announcement yet.
Why do not use .java instead of .xml as config file? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        There seems to be some work in-progress to provide Java Configuration option for Spring.

        Comment

        Working...
        X