河南网站建设华企祥云销售管理软件排名
通知可以根据切入点表达式来进行增强,也可以根据自己的注解值(例如 @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 注解中指定的日志类型来记录日志。
