Announcement Announcement Module
Collapse
No announcement yet.
InitializingBean afterPropertiesSet() is called twice??? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • InitializingBean afterPropertiesSet() is called twice???

    Why afterPropertiesSet() method of InitializingBean is called twice during spring start up?

    Code:
    @Service
    public class HelloBean implements InitializingBean{
    
    	private static final Logger log = Logger.getLogger(HelloBean.class);
    	
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		log.info("afterPropertiesSet() called!");
    	}
    
    	
    }
    
    Output-------
    - afterPropertiesSet() called!!!
    - afterPropertiesSet() called!!!

  • #2
    Without seeing your configuration, do you also have a bean configured in your xml for the same class? So you have two beans of this type and each one calls its afterPropertiesSet? Basically, there is something somewhere in your configuration that is making this happen. afterPropertiesSet is only called once when the bean is instantiated. Sometimes it might be because you set the scope to something besides singleton and therefore when you call getBean() it creates a new instance which will call afterPropertiesSet.

    For instance, if you have scope="prototype", everytime you call getBean() you get a new instance and Spring goes through the same lifecycle for the instantiation of that bean, which includes calling afterPropertiesSet after it is instantiate and DI happens and properties are set.

    Hope that helps

    Mark

    Comment


    • #3
      Mark,

      Your response was to the point! Thank you, the bean was loaded twice.

      Comment

      Working...
      X