在建议中跳过方法执行的用例是什么?

时间:2016-06-28 18:38:15

标签: spring aop spring-aop

我理解@Around建议的作用,当我们需要共享状态之前和之后,我们可以使用它,我们也调用跳过方法执行。我的问题是为什么Spring给了我们这种跳过方法执行的能力以及跳过方法的用例是什么?

2 个答案:

答案 0 :(得分:1)

Nándor所说的副作用是一回事。也许您甚至想要完全替换返回值,可能是因为类中没有源代码或其他原因的错误:

Buggy Java类:

package de.scrum_master.app;

public class Earth {
    public String getShape() {
        return "disc";
    }
}

驱动程序应用程序:

package de.scrum_master.app;

public class Application {
    public static void main(String[] args) {
        System.out.println("The Earth is a " + new Earth().getShape() + ".");
    }
}

控制台日志:

The Earth is a disc.

修正方面:

package de.scrum_master.aspect;

import de.scrum_master.app.Earth;

public aspect BugfixAspect {
    String around() : execution(* Earth.getShape()) {
        return "sphere";
    }
}

应用了方面的控制台日志:

The Earth is a sphere.

答案 1 :(得分:0)

方法调用通常有副作用。无论何时在您的方面决定这些副作用因任何原因都是不受欢迎的,它是一个有效的用例,可以跳过执行原始执行。这包括用于缓存的用例,例如,副作用不是数据方面,而是执行时间。

相关问题