spring Bean lifecycle

spring Bean lifecycle

public class InitSequenceBean implements InitializingBean {

public InitSequenceBean() {
  System.out.println("InitSequenceBean: constructor");

public void postConstruct() {
  System.out.println("InitSequenceBean: postConstruct");

public void initMethod() {
  System.out.println("InitSequenceBean: init-method");

public void afterPropertiesSet() throws Exception {
  System.out.println("InitSequenceBean: afterPropertiesSet");


InitSequenceBean: constructor

InitSequenceBean: postConstruct

InitSequenceBean: afterPropertiesSet

InitSequenceBean: init-method

They are all called sequentially in the ‘AbstractAutowireCapableBeanFactory’s

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                invokeAwareMethods(beanName, bean);
                return null;
        }, getAccessControlContext());
    else {
        invokeAwareMethods(beanName, bean);

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
 //this will iterate all BeanPostProcessors one of which(CommonAnnotationBeanPostProcessor) will execute @PostConstruct
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);

    try {
  //this will invoke afterPropertiesSet and any custom init-method
        invokeInitMethods(beanName, wrappedBean, mbd);
    catch (Throwable ex) {
        throw new BeanCreationException(
                (mbd != null ? mbd.getResourceDescription() : null),
                beanName, "Invocation of init method failed", ex);

    if (mbd == null || !mbd.isSynthetic()) {
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    return wrappedBean;

One comment

  1. Pingback: spring IoC life cycle | Life in USA

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s