Skip to content

Latest commit

 

History

History
88 lines (71 loc) · 5.27 KB

_33InitializingBean接口.md

File metadata and controls

88 lines (71 loc) · 5.27 KB

IoC 之深入分析 InitializingBean 和 init-method

Spring 的 org.springframework.beans.factory.InitializingBean 接口,为 bean 提供了定义初始化方法的方式,它仅包含了一个方法:#afterPropertiesSet() 。代码如下:

public interface InitializingBean {

    /**
     * 该方法在 BeanFactory 设置完了所有属性之后被调用
     * 该方法允许 bean 实例设置了所有 bean 属性时执行初始化工作,如果该过程出现了错误则需要抛出异常
     *
     * Invoked by the containing {@code BeanFactory} after it has set all bean properties
     * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
     * <p>This method allows the bean instance to perform validation of its overall
     * configuration and final initialization when all bean properties have been set.
     * @throws Exception in the event of misconfiguration (such as failure to set an
     * essential property) or if initialization fails for any other reason
     */
    void afterPropertiesSet() throws Exception;

}

AbstractAutowireCapableBeanFactory.doCreateBean()方法,该方法主要干三件事情:

  1. 实例化 bean 对象:#createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) 方法。
  2. 属性注入:#populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) 方法。
  3. 初始化 bean 对象:#initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) 方法。

而初始化 bean 对象时,也是干了三件事情:

  1. 激活 Aware 方法
  2. 后置处理器的应用
  3. 激活自定义的 init 方法

InitializingBean接口方法就是在 AbstractAutowireCapableBeanFactory.doCreateBean() 创建Bean,并初始化Bean的时候调用AbstractAutowireCapableBeanFactory.invokeInitMethods()从而触发InitializingBean初始化方法。代码如下:

// AbstractAutowireCapableBeanFactory.java

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
        throws Throwable {
    // 首先会检查是否是 InitializingBean ,如果是的话需要调用 afterPropertiesSet()
    boolean isInitializingBean = (bean instanceof InitializingBean);
    if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
        if (logger.isTraceEnabled()) {
            logger.trace("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) {
        // 判断是否指定了 init-method(),
        // 如果指定了 init-method(),则再调用制定的init-method
        String initMethodName = mbd.getInitMethodName();
        if (StringUtils.hasLength(initMethodName) &&
                !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                !mbd.isExternallyManagedInitMethod(initMethodName)) {
            // 激活用户自定义的初始化方法
            // 利用反射机制执行
            invokeCustomInitMethod(beanName, bean, mbd);
        }
    }
}
  • 首先,检测当前 bean 是否实现了 InitializingBean 接口,如果实现了则调用其 #afterPropertiesSet() 方法。
  • 然后,再检查是否也指定了 init-method,如果指定了则通过反射机制调用指定的 init-method 方法。

虽然该接口为 Spring 容器的扩展性立下了汗马功劳,但是如果真的让我们的业务对象来实现这个接口就显得不是那么的友好了,Spring 的一个核心理念就是无侵入性,但是如果我们业务类实现这个接口就显得 Spring 容器具有侵入性了。所以 Spring 还提供了另外一种实现的方式:init-method 方法

#invokeInitMethods(...) 方法中,我们知道 init-method 指定的方法会在 #afterPropertiesSet() 方法之后执行,如果 #afterPropertiesSet() 方法的执行的过程中出现了异常,则 init-method 是不会执行的,而且由于 init-method 采用的是反射执行的方式,所以 #afterPropertiesSet() 方法的执行效率一般会高些,但是并不能排除我们要优先使用 init-method,主要是因为它消除了 bean 对 Spring 的依赖,Spring 没有侵入到我们业务代码,这样会更加符合 Spring 的理念。诚然,init-method 是基于 xml 配置文件的,就目前而言,我们的工程几乎都摒弃了配置,而采用注释的方式,那么 @PreDestory 可能适合你,当然这个注解我们后面分析。

至此,InitializingBean 和 init-method 已经分析完毕了,对于DisposableBean 和 destroy-method ,他们和 init 相似,这里就不做阐述了。