• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Confused about bean's lifecycle method chain

 
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?!
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • 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.
 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • 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~~
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic