Spring aop 内部调用、自调用不生效问题与解决方案

场景

使用 spring cache 框架时 服务类内部方法调用并不触发缓存动作

演示

[[@Service](http://my.oschina.net/service)](http://my.oschina.net/service)
public class CacheTestService {


    @Cacheable(value = "test_cache", key = "'method'")
    public String method() {
        System.out.println("method");
        return method1(1) + "_" +
                method2(2) + "_" +
                method3(3) + "_" +
                method4(4)
                ;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    public String method1(int i) {
        System.out.println("method1");
        return "method1_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    protected String method2(int i) {
        System.out.println("method2");
        return "method2_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    String method3(int i) {
        System.out.println("method3");
        return "method3_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    private String method4(int i) {
        System.out.println("method4");
        return "method4_" + i;
    }
}

查看redis


127.0.0.1:6379> keys cache:*
1) "cache://test_cache:method"

可以看到 method1、method2、method3、method4 方法的缓存并没有生效

原因:

注意和限制 基于 proxy 的 spring aop 带来的内部调用问题 上面介绍过 spring cache 的原理,即它是基于动态生成的 proxy 代理机制来对方法的调用进行切面,这里关键点是对象的引用问题,如果对象的方法是内部调用 (即 this 引用)而不是外部引用,则会导致 proxy 失效,那么我们的切面就失效,也就是说上面定义的各种注释包括 @Cacheable、@CachePut 和 @CacheEvict 都会失效

解决方案


public interface BeanSelfAware {
    void setSelf(Object proxyBean);  
}  
@Component
public class InjectBeanSelfProcessor implements BeanPostProcessor, ApplicationContextAware {
    private ApplicationContext context;
    //① 注入ApplicationContext  
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;  
    }  
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {  
        if(!(bean instanceof BeanSelfAware)) { //② 如果Bean没有实现BeanSelfAware标识接口 跳过  
            return bean;  
        }  
        if(AopUtils.isAopProxy(bean)) { //③ 如果当前对象是AOP代理对象,直接注入
            ((BeanSelfAware) bean).setSelf(bean);  
        } else {  
            //④ 如果当前对象不是AOP代理,则通过context.getBean(beanName)获取代理对象并注入  
            //此种方式不适合解决prototype Bean的代理对象注入  
            ((BeanSelfAware)bean).setSelf(context.getBean(beanName));  
        }  
        return bean;  
    }  
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {  
        return bean;  
    }  
}  

服务类:

@Service
public class CacheTestService implements BeanSelfAware {


    @Cacheable(value = "test_cache", key = "'method'")
    public String method() {
        System.out.println("method");
        return proxySelf.method1(1) + "_" +
                proxySelf.method2(2) + "_" +
                proxySelf.method3(3) + "_" +
                proxySelf.method4(4)
                ;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    public String method1(int i) {
        System.out.println("method1");
        return "method1_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    protected String method2(int i) {
        System.out.println("method2");
        return "method2_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    String method3(int i) {
        System.out.println("method3");
        return "method3_" + i;
    }

    @Cacheable(value = "test_cache", key = "'method'+#i")
    private String method4(int i) {
        System.out.println("method4");
        return "method4_" + i;
    }


    CacheTestService proxySelf;

    @Override
    public void setSelf(Object proxyBean) {
        this.proxySelf = (CacheTestService) proxyBean;
    }
}

再次查看 redis


127.0.0.1:6379> keys cache:*
1) "cache://test_cache:method1"
2) "cache://test_cache:method"

方法 method1 缓存动作成功执行,但是以protected、默认、private修饰的方法签名并没有生效 因此 此方案还需注意使用public修饰被调用方法

...

骚年,你以为这样就完了? 不 还有坑!

启用 aop 时 请使用

    <!-- 用这个 -->
    <aop:aspectj-autoproxy proxy-target-class="true" /> 
   <!-- 不要用这个 -->
    <!--<aop:config proxy-target-class="true"  />-->

over

参考资料

转载于:https://my.oschina.net/longyuan/blog/714355

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值