dede网站后台模板,国外ip代理app,网站百度关键词排名软件,广州微网站建设通知可以根据切入点表达式来进行增强#xff0c;也可以根据自己的注解值#xff08;例如 Before、After、Around 等#xff09;来进行增强。
如果要根据切入点表达式来进行增强#xff0c;需要在通知注解中使用 Pointcut 注解来引用切入点表达式。例如#xff0c;在以下示…通知可以根据切入点表达式来进行增强也可以根据自己的注解值例如 Before、After、Around 等来进行增强。
如果要根据切入点表达式来进行增强需要在通知注解中使用 Pointcut 注解来引用切入点表达式。例如在以下示例中我们使用 Before 和 After 注解来分别在方法执行前和执行后增强 SomeService 类中的 doSomething 方法
Aspect
Component
public class SomeAspect {Pointcut(execution(* com.example.demo.service.SomeService.doSomething(..)))public void doSomethingPointcut() {}Before(doSomethingPointcut())public void beforeDoSomething() {System.out.println(before doSomething);}After(doSomethingPointcut())public void afterDoSomething() {System.out.println(after doSomething);}
}如果要根据自己的注解值来进行增强则可以直接在通知注解中指定增强的类型。例如在以下示例中我们使用 MyLog 注解来标记需要记录日志的方法在 MyLog 注解的 value 属性中指定日志类型然后使用 Around 注解来拦截该方法并记录日志
Aspect
Component
public class LoggingAspect {Around(annotation(com.example.demo.annotation.MyLog))public Object logMethod(ProceedingJoinPoint joinPoint) throws Throwable {String methodName joinPoint.getSignature().getName();String logType ((MyLog) joinPoint.getTarget().getClass().getMethod(methodName, Arrays.stream(joinPoint.getArgs()).map(Object::getClass).toArray(Class[]::new)).getAnnotation(MyLog.class)).value();System.out.println(start logType log);Object result joinPoint.proceed();System.out.println(end logType log);return result;}
}在这个示例中我们定义了一个 MyLog 注解用于标记需要记录日志的方法。MyLog 注解有一个 value 属性用于指定日志类型。然后在 LoggingAspect 类中我们使用 Around 注解来拦截标记了 MyLog 注解的方法并根据 MyLog 注解中指定的日志类型来记录日志。