Announcement Announcement Module
Collapse
No announcement yet.
reload / refresh XmlBeanFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • reload / refresh XmlBeanFactory

    hi,

    I'm quiet new to spring so maybe it's a trivial question.

    Is there a way to reload a XmlBeanFactory when the underlying beans.xml has been changed ?[/code]

  • #2
    I am very interested in this too - does Spring have some Observer/Observable pattern going on with the config files?

    Comment


    • #3
      I found another topic suggesting that one would need to write a custom application context to accomplish this.

      http://forum.springframework.org/showthread.php?t=10432

      This seems odd. My current application can dynamicly reload it's configuration (albeit you have to invoke a servlet that performs the necessary actions), and I need to be able to do the same thing with Spring. One would normally hot deploy the webapp and it would all be reloaded anyway, but unfortunately we are not in the web container.
      All our current configuration is established by a Singleton class that has a reload() method. I suppose we could just convert that class into a Spring Bean and let all the other classes refer to him via DI, but it seems conter-intuitive to using Spring, since the config for all the Spring beans would reside in our own, custom and proprietary type of config file just to get the reloadablity.
      Has anyone worked on / solved this reloadablity issue?
      Last edited by robyn; May 19th, 2006, 06:15 AM.

      Comment


      • #4
        I found kind of a solution using FileSystemXmlApplicationContext
        I use a FileWatchDog (org.apache.log4j.helpers.FileWatchdog for example) on my spring beans.xml file and when the file has been changed calll the refresh method on the context.

        This works but I don't know if there's a way to do it directly with Spring (meaning Spring detecting that the file has changed)

        Comment


        • #5
          That solves the refreshing problem. Nice

          Would you be willing to post some code?

          Comment


          • #6
            so I have an inner static inner class that inherits from log4j FileWatchDog

            Code:
                private static class DiagnosticFileWatcher extends FileWatchdog
                {
                            
                    DiagnosticFileWatcher(String fileName)
                    {
                        super( fileName);
                    }
                          
                    protected void doOnChange() 
                    {            
                        
                        reloadConfig();           
                    }       
                }

            in the main class I have something like this :

            Code:
              
                public static String SYS_DIAGNOSTIC_CONFIG_FILENAME = "com.something.framework.diagnostics.XMLConfigFile";
                private static String configFileName;
                
                private static DiagnosticFileWatcher fileWatcher =  null;
                
                public static final long FILE_CHECK_INTERVAL = 60 * 1000;   
            
                static 
                {
                	//depending on a system property
                	configFileName = System.getProperty(SYS_DIAGNOSTIC_CONFIG_FILENAME);
                	
                	// initial loadConfig not necessary will be done by the fileWatcher
                	
                	fileWatcher = new DiagnosticFileWatcher(configFileName);
                	fileWatcher.setDelay(FILE_CHECK_INTERVAL);
                	fileWatcher.start();    	
                }
            
            
                public static void reloadConfig()
                {
                    if(diagnosticsContext != null)
                        diagnosticsContext.refresh();
                    else
                        initFromFile(configFileName, true);
                }
            
                public static void initFromFile(String fileName, boolean refresh)
                {            
                    
                	if(diagnosticsContext == null)
                	{
                        String[] configLocations = {configFileName};
                        diagnosticsContext = new FileSystemXmlApplicationContext(configLocations, true);
                	}
                	else if(refresh)
                	{
                	    diagnosticsContext.refresh();
                	}
                }
            voila that's not rocket science, I'm not completetly sure it's the best way to do it but it works



            if you have suggestions to improve it let me know

            Comment

            Working...
            X