Results 1 to 7 of 7

Thread: Reloading a Specific Spring Bean

  1. #1

    Default 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. #2
    Join Date
    Oct 2008
    Posts
    1

    Default

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

  3. #3
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    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.
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

  4. #4

    Default

    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);

  5. #5
    Join Date
    Aug 2010
    Location
    Ohio-USA
    Posts
    5

    Default

    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 at 05:42 PM.

  6. #6

    Default

    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);

  7. #7
    Join Date
    Aug 2010
    Location
    Ohio-USA
    Posts
    5

    Default Thank you...

    It does work.

Posting Permissions

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