BeanPostProcessor InitializingBean

BeanPostProcessor

/**
*BeanPostProcessor 为每个bean实例化时提供个性化的修改,做些包装等
**/
public interface BeanPostProcessor {

   //在bean实例化前调用

    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

/**
*   在bean实例化后调用,如果bean实现了InitializingBean,则在执行完*  该接口的afterPropertiesSet方法后调用 ,如果实现了init-method则
* 在执行完init-method后调用
*/   
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

    }

InitializingBean

/*
*InitializingBean 为实现该接口的bean提供默认的初始化方法
*也可以在xml配置bean的使用init-method来实现初始化方法
*/
public interface InitializingBean {

    void afterPropertiesSet() throws Exception;

}

bean在初始化过程中 BeanPostProcessor 和 InitializingBean 由application调用的顺序

bean的最终初始化是由AbstractAutowireCapableBeanFactory的initializeBean方法来完成的,下面是该方法的源码

    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()) {
            wrappedBean = **applyBeanPostProcessorsBeforeInitialization**(wrappedBean, beanName);
        }

        try {
            **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;
    }

可以看到initializeBean主要有四步骤
1. invokeAwareMethods 调用实现Aware接口方法,这里只针对三种Aware接口BeanNameAware,BeanClassLoaderAware,BeanFactoryAware,方法如下

    private void invokeAwareMethods(final String beanName, final Object bean) {
        if (bean instanceof Aware) {
            if (bean instanceof BeanNameAware) {
                ((BeanNameAware) bean).setBeanName(beanName);
            }
            if (bean instanceof BeanClassLoaderAware) {
                ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
            }
            if (bean instanceof BeanFactoryAware) {
                ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
            }
        }
    }
  1. applyBeanPostProcessorsBeforeInitialization 调用所有实现BeanPostProcessor类 的 postProcessBeforeInitialization方法,源码如下
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessBeforeInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }
  1. invokeInitMethods 调用bean的初始化方法,如果bean实现了InitializingBean 接口,则调用afterPropertiesSet方法,如果有配置init-method,则调用配置的初始化方法,源码如下:
    protected void invokeInitMethods(String beanName, final Object bean, 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(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                            ((InitializingBean) bean).afterPropertiesSet();
                            return null;
                        }
                    }, getAccessControlContext());
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }               
            else {
                ((InitializingBean) bean).**afterPropertiesSet**();
            }
        }

        if (mbd != null) {
            String initMethodName = mbd.getInitMethodName();
            if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                    !mbd.isExternallyManagedInitMethod(initMethodName)) {
                **invokeCustomInitMethod**(beanName, bean, mbd);
            }
        }
    }
  1. applyBeanPostProcessorsAfterInitialization 调用所有实现BeanPostProcessor类 的 postProcessAfterInitialization方法,源码如下
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessAfterInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值