This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about bean's lifecycle method chain

 
Sain Euwhyn
Greenhorn
Posts: 2
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • 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?!
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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~~
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic