• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about bean's lifecycle method chain

 
Greenhorn
Posts: 2
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



-----------------------------------------------------------------------------------------
why the output is:


0. Spring calls constructor
1. Spring setter DI:1
2. BeanNameAware#setBeanName:2
3. BeanFactoryAware#setBeanFactory:3
4. ApplicationContextAware#setApplicationContext:4
1320491454906
6. InitializingBean#afterPropertiesSet:5

6'. bean#init:6
1320491454921
5. BeanPostProcessor#postProcessBeforeInitialization:7

7. BeanPostProcessor#postProcessAfterInitialization:8



According to <<Spring in Action 3rd>> and Spring-Reference:


6 If any of the beans implement the BeanPostProcessor interface, Spring calls
their postProcessBeforeInitialization() method.
7 If any beans implement the InitializingBean interface, Spring calls their
afterPropertiesSet() method. Similarly, if the bean was declared with an
init-method, then the specified initialization method will be called.



It seems that there is something wrong with the order?!
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BeanPostProcessor are special beans that are created before any other (regular) beans are created.

postProcessBeforeInitialization will not be applied on the BeanPostProcessor itself but is called once for every other bean that is created.

In you example configuration if you remove the other bean
<bean id="holder" class="java.lang.String" />you will find that the sysout will not contain

5. BeanPostProcessor#postProcessBeforeInitialization:7
7. BeanPostProcessor#postProcessAfterInitialization:8


Similarly if you add multiple beans you will see the post processor being called once for each Bean!


The rule,

If any of the beans implement the BeanPostProcessor interface, Spring calls
their postProcessBeforeInitialization() method.
7 If any beans implement the InitializingBean interface, Spring calls their
afterPropertiesSet() method. Similarly, if the bean was declared with an
init-method, then the specified initialization method will be called.



applies for regular beans not for bean that themselves implement the BeanPostProcessor interface.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the CodeRanch, Sain Euwhyn!
 
Sain Euwhyn
Greenhorn
Posts: 2
Firefox Browser Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much Sam Mercs!~
I'm new to Spring, you just helped me about my problem which puzzled me days, and I couldn't find proper answer from <<Spring in Action>>.
By the way, thanks Vijitha also, you are very hospitable! And I'm appologize for the late reply~~
 
reply
    Bookmark Topic Watch Topic
  • New Topic