一、BeanPostProcessor
BeanPostProcessor,實現這個接口之後允許對spring管理的bean進行增強處理。該接口主要有兩個方法
Object postProcessBeforeInitialization(Object bean, String name) throws BeansException;
Object postProcessAfterInitialization(Object bean, String name) throws BeansException;
這兩個方法的調用時機,
postProcessBeforeInitialization
|
bean的初始化過程
|
postProcessAfterInitialization
實際當中,spring提供了大量的bean後處理器,其中比較常用的兩個bean後處理器就有
1、BeanNameAutoProxyCreator,這個是根據bean的名稱,創建bean的實例代理
2、DefaultAdvisorAutoProxyCreator,這個是對容器中的所有實例創建代理,其中shiro根據註解對方法進行的權限管理就是基於這個處理器。
二、shiro怎麼用的bean後處理器
1、DefaultAdvisorAutoProxyCreator的父類
AbstractAutoProxyCreator其中實現了postProcessAfterInitialization,在這個方法中,爲spring中的bean創建代理類
protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource)
2、創建aop代理可以看到,根據配置是創建jdk代理或者cglib代理,兩者的區別一個基於接口,一個創建出子類 public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<?> targetClass = config.getTargetClass(); if (targetClass == null) { throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } }
3、其實shiro是cglib形式代理的後面再專門講下cglib代理,簡單以JdkDynamicAopProxy爲例,可以發現該類實現了InvocationHandler,這個類怎麼用在《JAVA編程思想》有一塊篇幅專門講的。並且還有實例,具體就是根據接口創建的代理,需綁定具體實現類,以接口調用方法時,會執行實現了InvocationHandler裏的invoke方法,這樣就可以在調用方法前後執行aop操作。
JdkDynamicAopProxy的invoke方法裏面
// We need to create a method invocation... invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain. retVal = invocation.proceed(); ReflectiveMethodInvocation對象中的proceed方法就是具體執行的方法了。實現了
MethodInterceptor就可以針對bean操作就行注入操作。