Announcement Announcement Module
Collapse
No announcement yet.
Reloading a Specific Spring Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reloading a Specific Spring Bean

    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?

  • #2
    I'm interrested by the solution, if somebody knows...

    Comment


    • #3
      The only thing I was able to find on this is chapter 24.3.1.2. "Refreshable beans" in the Spring manual. But that's using dynamic languages such as Ruby or Groovy.

      What you could do is provide a Refreshable interface, implement the refresh behavior and write a method that gets a bean, tries to cast it to a Refreshable and if succeeds, calls the implemented refresh method.

      This, by the way, is more a general Spring question and isn't RCP specific. You could try to ask this question in the general forum.

      Comment


      • #4
        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);

        Comment


        • #5
          Thank you Sudheesh.Sorry I am new to Spring. actually we have a situation where in we want to assign different implementation class to the bean which is declared in spring conf file. you have any idea?
          Last edited by ramadossnambi; Aug 10th, 2010, 06:42 PM.

          Comment


          • #6
            I dont think its a good idea to change the BEAN DEFENITION in runtime if you dont have a real reason to do that. My suggestion would be

            1) bean1 with CLASS1
            2) bean2 with CLASS2

            where CLASS1 and CLASS2 are two implemenations of same INTERFACE

            Based on need use the beans

            or else as explained above use beanDefinition.setBeanClassName(className);

            Comment


            • #7
              Thank you...

              It does work.

              Comment

              Working...
              X