| Author |
Confused about bean's lifecycle method chain
|
Sain Euwhyn
Greenhorn
Joined: Nov 05, 2011
Posts: 2
|
|
-----------------------------------------------------------------------------------------
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
Joined: Feb 08, 2009
Posts: 576
|
|
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.
|
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
|
And welcome to the CodeRanch, Sain Euwhyn!
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Sain Euwhyn
Greenhorn
Joined: Nov 05, 2011
Posts: 2
|
|
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~~
|
 |
 |
|
|
subject: Confused about bean's lifecycle method chain
|
|
|