美文网首页
SpringCache工作过程

SpringCache工作过程

作者: huiwq1990 | 来源:发表于2017-07-01 10:06 被阅读0次

如果类中有@Cacheable等注解, 响应的Bean会被代理掉;如果没有则不会。

@EnableCaching作用

引入配置类org.springframework.cache.annotation.ProxyCachingConfiguration
然后初始化三个Bean:
BeanFactoryCacheOperationSourceAdvisor 后面创建代理时会用到
CacheOperationSource
CacheInterceptor

Bean的Cache代理类

断点 org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean 73

image.png

后置增强处理

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

    protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
        if (System.getSecurityManager() != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                @Override
                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);
        }
// bean初始化后置处理器,将bean进行代理
        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }
        return wrappedBean;
    }

遍历BeanPostProcessor

    @Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
// 遍历所有的BeanPostProcessor
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessAfterInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }

InfrastructureAdvisorAutoProxyCreator生成代理类

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessary

// 会返回一个BeanFactoryCacheOperationSourceAdvisor
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
        if (specificInterceptors != DO_NOT_PROXY) {
            this.advisedBeans.put(cacheKey, Boolean.TRUE);
            Object proxy = createProxy(
                    bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
            this.proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        }

参考

http://www.cnblogs.com/davidwang456/p/5703037.html
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/

相关文章

网友评论

      本文标题:SpringCache工作过程

      本文链接:https://www.haomeiwen.com/subject/avewcxtx.html