Announcement Announcement Module
No announcement yet.
Refreshing /Reloading a specific spring bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Refreshing /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
    Originally posted by Sudheesh View Post

    But how can we Refresh a SPECIFIC bean?
    Very popular question last days.. Why doesn't everyone want to use the forum search...


    • #3

      I have not found answer for my question

      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"


      • #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);
        defaultListableBeanFactory.registerBeanDefinition( beanName, beanDefinition);


        • #5
          Hi All,

          I have the following two bean definitions in my application context.

          Suppose, the user can change the following params at runtime:
          1. the cronExpression of the cronTrigger bean
          2. the mail.from of the mailSender bean

          What is the best way to refresh those new property values in the beans already instantiated in Spring ?


          <!-- Cron Trigger -->
          <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronT riggerBean">
          <property name="jobDetail" ref="schedulerEmailJob" />
          <property name="cronExpression">

          <!-- Email Sender -->
          <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
          <property name="host" >
          <property name="port" >
          <property name="username" >
          <property name="password" >
          <property name="javaMailProperties">
          <prop key="mail.smtp.auth">${mailServer.smtp.auth}</prop>
          <prop key="mail.from">${}</prop>

          <!-- prop key="mail.smtp.starttls.enable">true</prop-->