Spring 多次调用 AOP 建议

Spring AOP advice is called several times

我有一个方法需要在执行前后进行拦截,所以为了做到这一点我使用了AspectJ

效果很好,但由于某些原因多次调用该建议。

查看调用层次结构,我注意到当调用 joinPoint.proceed(); 而不是继续执行方法时,我再次进入建议方法。

虽然我只调用了一次 getCacheableSite(String siteId),但我不明白为什么建议方法被调用了几次。

Spring xml 配置:

<bean id="runtimeCacheAspect" class="site.aspect.RuntimeCacheAspect">
<aop:aspectj-autoproxy />

<aop:config>
    <aop:aspect id="aspectLogging" ref="runtimeCacheAspect" >
        <!-- @Around -->
        <aop:pointcut id="pointCutAround"
                      expression="execution(public * getCacheableSite(..))" />
        <aop:around method="getData" pointcut-ref="pointCutAround" />

    </aop:aspect>
</aop:config>

建议:

public Object getData(ProceedingJoinPoint joinPoint) throws Throwable {
    Object returnObject = null;
    try {
        System.out.println("Before");
        returnObject = joinPoint.proceed();
    } catch (Throwable throwable) {
        throw throwable;
    }
    finally {
        System.out.println("After");
    }
    return returnObject;

需要拦截的方法:

    public JsonObject getCacheableSite(String siteId) {
        System.out.println("Method being executed..."); 
}

输出:

Before
Before
Before
Before
Before
Before
Before
Before
Before
Before
Method being executed...
After
After
After
After
After
After
After
After
After
After

这解决了我的问题:

    <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true">
        <aop:include name="getCacheableSite" />
    </aop:aspectj-autoproxy>