Announcement Announcement Module
Collapse
No announcement yet.
Reloading ApplicationContext created by ContextLoaderListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reloading ApplicationContext created by ContextLoaderListener

    In my web-app the ApplicationContext is made up of several config files where one of them has been externalized and put in a directory outside the web-app. This lets me reconfigure some beans without the need of rebuilding the entire app.

    I have also implemented a web service (within the same app) that can read from and write to this external spring-bean.xml file. This (almost) lets me reconfigure the application while it is runnig by passing the updated bean configuration as parameter to the web service.

    The only catch is that I manually have to restart the web container because I can't figure out how to reload the ApplicationContext. Ideally I would like the ApplicationContext to be reloaded every time i submit a new configuration to the above mentioned Web Service.

    I am using ContextLoaderListener (defined in web.xml) to load my application context - is there a way to force it to drop the old context and load it over again? If not, does anyone know another way to achieve my goal which is to update one of my beans.xml -files and reload the entire context?


    Thanks,

    Geir

  • #2
    Well only reloading your root ApplicationContext isn't going to work. You also need to instruct all the beans/classes that use a bean from the root application context to reload/reinitialize itself, else you might hang on to old instances (or you need to use remoting for everything which eliminates that).

    You could try and use JMX, create a class that is ApplicationContextAware, make some methods to stop/start/restart the ApplicationContext. Register that bean in the ApplicationContext, expose it with JMX and you should be in business for the reloading. Refreshing all the other beans in childs and static accessor (WebApplicationContextUtils.get*().getBean() in servlets for instance) need to be informed also.

    So IMHO you are probably faster and easier of by restarting your application.

    Comment


    • #3
      And what about reloading one single bean? I just added one attribute into one bean definition in xml and don't want to restart whole container. Is that feasible? Thanks.

      Comment


      • #4
        You still have the same issue... You will need to inform all the other beans that the instance they use isn't valid anymore.

        Changing 1 bean or 100 beans doesn't make the problem different or go away .

        Comment


        • #5
          Refresh/Reload Spring context dynamically

          Spring is providing a method by which we can dynamically REFRESH/RELOAD entire context .

          Create an ApplicationContextAware bean with following functions

          /**
          * setApplicationContext
          */
          public void setApplicationContext(ApplicationContext applicationContext)
          throws BeansException {
          this.applicationContext = applicationContext;

          }
          /**
          * refreshContext()
          */
          public void refreshContext(){
          ((ConfigurableApplicationContext)applicationContex t).refresh();
          }


          And call refreshContext() for the purpose.




          But how can we Refresh a SPECIFIC bean?

          Comment


          • #6
            Suppose the single bean is with ZERO DEPENDENCY


            I have setup

            1) Mbean stuff in my spring application

            2) mechanism by which reload entire context
            when updating config files (bean xml files)


            Now I need a way by which i can reload a single bean (suppose its with zero dependency)



            I can not find any method for " Reload a single bean when its bean configuration file is changed"





            Spring is providing a method by which we can dynamically REFRESH/RELOAD entire context .

            Create an ApplicationContextAware bean with following functions

            /**
            * setApplicationContext
            */
            public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
            this.applicationContext = applicationContext;

            }
            /**
            * refreshContext()
            */
            public void refreshContext(){
            ((ConfigurableApplicationContext)applicationContex t).refresh();
            }


            And call refreshContext() for the purpose.




            But how can we Refresh a SPECIFIC bean?

            Comment


            • #7
              Refresh a SPECIFIC bean...

              Originally posted by Sudheesh View Post
              But how can we Refresh a SPECIFIC bean?
              How about re-registerBeanDefinition?

              The following is what I use for changing .class of the a specific bean.

              Code:
              	public static void changeBeanClass(String beanName, String className) throws ClassNotFoundException, LinkageError {
              		XmlWebApplicationContext xmlWebApplicationContext =
              				(XmlWebApplicationContext) ContextLoader.getCurrentWebApplicationContext();
              		DefaultListableBeanFactory defaultListableBeanFactory =
              				(DefaultListableBeanFactory) xmlWebApplicationContext.getBeanFactory();
              		BeanDefinition beanDefinition = defaultListableBeanFactory.getBeanDefinition(beanName);
              		beanDefinition.setBeanClassName(className);
              		defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinition);
              	}
              Last edited by Jang Joonbo; Oct 10th, 2008, 04:34 AM.

              Comment


              • #8
                need some inputs

                Hi

                We are trying to achieve the same in our application. i.e reloading a single bean from spring context. Do you have any inputs on that? Your inputs are appreciated.

                Thanks & Regards
                RAM

                Comment


                • #9
                  If you want to reload a specific bean class on a file change

                  XmlWebApplicationContext xmlWebApplicationContext =
                  (XmlWebApplicationContext) ContextLoader.getCurrentWebApplicationContext();
                  DefaultListableBeanFactory defaultListableBeanFactory =
                  (DefaultListableBeanFactory) xmlWebApplicationContext.getBeanFactory();
                  BeanDefinition beanDefinition = defaultListableBeanFactory.getBeanDefinition(beanN ame);
                  beanDefinition.setBeanClassName(className);
                  defaultListableBeanFactory.registerBeanDefinition( beanName, beanDefinition);

                  (http://forum.springsource.org/showthread.php?t=61495)

                  But if bean is having dependency then this is not advisable

                  Comment

                  Working...
                  X