Notes the role of LifecycleBeanPostProcessor of shiro

In a system using springboot integration shiro, the configuration bean, it tends to be configured LifecycleBeanPostProcessor, get to the bottom of this there has been no specific action, but probably know some of shiro bean is used to manage the life cycle. Today checked the source code, finally uncover the mystery of it.

Let's look at the daily configuration:

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }


    @Bean
    @DependsOn({"lifecycleBeanPostProcessor"})
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

DefaultAdvisorAutoProxyCreator is used to scan context, looking for all the Advistor (notifier), the Advisor will be applied to all eligible entry point of the Bean. LifecycleBeanPostProcessor must be created after creation, so add

@DependsOn({"lifecycleBeanPostProcessor"})

Guarantee create LifecycleBeanPostProcessor Before creating DefaultAdvisorAutoProxyCreator.

Look at LifecycleBeanPostProce:

public class LifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor, PriorityOrdered {

         .........................
         //省略。。。


    public Object postProcessBeforeInitialization(Object object, String name) throws BeansException {
        if (object instanceof Initializable) {
                ((Initializable) object).init();
        }
        return object;
    }


    public Object postProcessAfterInitialization(Object object, String name) throws BeansException {
        // Does nothing after initialization
        return object;
    }

    public void postProcessBeforeDestruction(Object object, String name) throws BeansException {
      if (object instanceof Destroyable) {
             ((Destroyable) object).destroy();
        }
    }
}

Point into the source code found in this class implements DestructionAwareBeanPostProcessor, what DestructionAwareBeanPostProcessor that? And then get to the bottom and found

This is already provided by the spring

public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {

    void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
}

BeanPostProcessor: 

public interface BeanPostProcessor {
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

Look at spring time in the bean initialization:

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
        //省略。。。

        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
        }

        //省略。。。

            invokeInitMethods(beanName, wrappedBean, mbd);

       //省略。。。

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

And wherein applyBeanPostProcessorsBeforeInitialization applyBeanPostProcessorsAfterInitialization is performed on the corresponding LifecycleBeanPostProcessor of postProcessBeforeInitialization and postProcessAfterInitialization, just to give an example of the initialization, destruction bean Similarly available.

Since then, the Initializable know LifecycleBeanPostProcessor Destroyable and implementation classes are unified in its internal automatic call Initializable.init () and Destroyable.destroy () method, so as to achieve management shiro bean life cycle.
 

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/93750520