Nic*_*rta 5 java aop aspectj spring-boot
这是我第一次接触AOP。我有一个带有一个方面的 spring-boot 应用程序,一个记录器。搜索我得出的结论是 @Around 方法在方法之前和之后执行(我只在一个方法中调用它),这是对的吗?在我的 @Around 方法中间,有一个joinPoint.proceed(). 如果我没记错的话,这JoinPoint是我必须用来获取调用方面的方法的信息的对象,但我无法理解进程实际上在做什么!
这是我的代码:
@Around(value = "execution(* *(..)) && @annotation(Loggable)", argNames = "ProceedingJoinPoint, Loggable")
public Object logAround(ProceedingJoinPoint joinPoint, Loggable loggable) throws Throwable {
String methodArguments = loggable.printMethodArguments() ? Arrays.toString(joinPoint.getArgs()) : "[]";
long start = System.currentTimeMillis();
Object returnObject = joinPoint.proceed(); // continue on the
// intercepted method
long elapsedTime = System.currentTimeMillis() - start;
String returnValue = loggable.printReturn() && returnObject != null ? returnObject.toString() : "[]";
LOG.info("Logging method: {}.{} Method arguments: {}. Method return value: {}. Method execution time: {}",
joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName(), methodArguments,
returnValue, elapsedTime);
return returnObject;
}
Run Code Online (Sandbox Code Playgroud)
正如你所说,当你使用@Around它时,就像你可以在方法之前做任何你想做的事情,然后调用该方法,然后你可以在调用方法之后做任何你想做的事情。
//Read file, Log , .... (Before method calling)
//Invoke the method (joinPoint.proceed)
//Write to the file, complete log, .... (After method calling)
Run Code Online (Sandbox Code Playgroud)
调用阶段由 完成joinPoint.proceed()。
1-调用方法之前记录
2-调用或调用您在其上设置切入点的方法(proceed)
3-将日志保存到数据库或写入文件或发送,...
在这个示例中,使用@Around,您可以对用户进行授权并确定他们是否可以使用该方法?
所以在方法调用之前需要进行授权过程,如果授权true则调用方法,如果没有则抛出异常或者可以登录。
1-调用方法之前授权用户
2- 如果授权true则调用方法 ( joinPoint.proceed();)
总之,joinPoint.proceed();意味着您正在调用 set 方法,或者调用它。
| 归档时间: |
|
| 查看次数: |
10807 次 |
| 最近记录: |