一、spring类初始化@PostConstruct > InitializingBean > init-method

  InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的子类,在初始化bean的时候会执行该方法。

示例

<bean id="myInitializingBean" class="com.paic.phssp.springtest.init.MyInitializingBean" init-method="testInit"></bean>

bean

/**
 * 继承InitializingBean接口的类,在初始化bean的时候会执行该方法
 */
//@Component
public class MyInitializingBean implements InitializingBean {

    public MyInitializingBean() {
        System.out.println("1MyInitializingBean....");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("3ceshi MyInitializingBean>>>>>>>>>>>>>>>>>>>");
    }

    @PostConstruct  //功能上近似init-method,但加载时机不同
    public void test(){
        System.out.println("2PostConstruct >>>>>>>>>>>>");
    }

    public void testInit(){
        System.out.println("4ceshi init-method");
    }
}

结果:

1MyInitializingBean....
2PostConstruct >>>>>>>>>>>>
3ceshi MyInitializingBean>>>>>>>>>>>>>>>>>>>
4ceshi init-method

说明:

  通过上述输出结果,三者的先后顺序:Constructor > @PostConstruct > InitializingBean > init-method

1.1、 InitializingBean > init-method 执行时机

  spring初始化bean过程

  002-创建型-03-单例模式(Singleton)【7种】、spring单例及原理  spring 单例

  通过查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出

  如使用getBean方式创建:getBean→doGetBean→createBean→doCreateBean→initializeBean→invokeInitMethods

  查看AbstractAutowireCapableBeanFactory.invokeInitMethods

    protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
            throws Throwable {

        boolean isInitializingBean = (bean instanceof InitializingBean);
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (logger.isDebugEnabled()) {
                logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
            }
            if (System.getSecurityManager() != null) {
                try {
                    AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                        ((InitializingBean) bean).afterPropertiesSet();
                        return null;
                    }, getAccessControlContext());
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }
            else {
                ((InitializingBean) bean).afterPropertiesSet();
            }
        }

        if (mbd != null && bean.getClass() != NullBean.class) {
            String initMethodName = mbd.getInitMethodName();
            if (StringUtils.hasLength(initMethodName) &&
                    !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                    !mbd.isExternallyManagedInitMethod(initMethodName)) {
                invokeCustomInitMethod(beanName, bean, mbd);
            }
        }
    }
View Code

相关文章:

  • 2021-06-27
  • 2022-12-23
  • 2021-11-01
  • 2021-11-28
  • 2021-05-23
  • 2022-02-06
  • 2021-12-26
  • 2021-10-20
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
  • 2021-07-14
  • 2023-03-31
  • 2021-05-18
相关资源
相似解决方案