Java的AspectJ代码举例
时间: 2023-05-12 10:05:15 浏览: 62
以下是一个简单的AspectJ代码示例:
```
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before executing " + joinPoint.getSignature().getName() + " method");
}
@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("After executing " + joinPoint.getSignature().getName() + " method with result " + result);
}
@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "exception")
public void logAfterThrowing(JoinPoint joinPoint, Throwable exception) {
System.out.println("Exception thrown by " + joinPoint.getSignature().getName() + " method with message " + exception.getMessage());
}
}
```
这是一个简单的日志切面,它会在执行 com.example.service 包中的任何方法之前、之后和抛出异常时打印日志。