问题背景

在Spring AOP中,如果一个bean被代理,那么在目标bean内部,通过AopContext.currentProxy()可以获取到代理对象自身。有时候,我们希望在同一个bean的不同方法中调用对方时,调用的是代理方法并能够生效缓存。然而,直接在同一个类的不同方法中调用方法时,缓存可能并不生效。那么,我们如何使用AopContext.currentProxy()来解决这个问题呢?

解决方案

要解决缓存不生效的问题,可以使用以下的步骤:

步骤一:配置Spring AOP

首先,在Spring配置文件中配置AOP,确保目标bean被代理。如下所示:

<!-- 配置AOP -->
<aop:aspectj-autoproxy />

<!-- 配置目标bean -->
<bean id="targetBean" class="com.example.TargetBean" />

步骤二:在目标bean类中使用AopContext.currentProxy()

在目标bean类中的方法A中调用方法B时,使用AopContext.currentProxy()来获取代理对象,并在这个代理对象上调用方法B。如下所示:

public class TargetBean implements Target {

    @Autowired
    private Target self;

    // 方法A
    @Override
    public void methodA() {
        Target proxy = (Target) AopContext.currentProxy();
        proxy.methodB(); // 在代理对象上调用方法B
    }

    // 方法B
    @Cacheable("cacheName")
    public void methodB() {
        // 执行业务逻辑
    }
}

步骤三:设置exposeProxy为true

为了使AopContext.currentProxy()能够返回代理对象,需要在Aop配置中将exposeProxy设置为true,以便将代理对象暴露给AopContext。如下所示:

<aop:config>
    <aop:aspect id="cacheAspect" ref="cacheAdvice">
        <!-- 设置exposeProxy为true -->
        <aop:pointcut id="targetBeanMethods" expression="execution(* com.example.TargetBean.*(..))" />
        <aop:around pointcut-ref="targetBeanMethods" method="addAdvice">
        </aop:around>
    </aop:aspect>
</aop:config>

通过上述步骤,AopContext.currentProxy()就能够返回正确的代理对象,从而解决了缓存不生效的问题。