编写一段Java语言切面示例代码
时间: 2023-04-10 16:04:12 浏览: 67
以下是一个简单的Java切面示例代码:
```java
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.myapp.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before executing " + joinPoint.getSignature().getName());
}
@AfterReturning("execution(* com.example.myapp.service.*.*(..))")
public void logAfterReturning(JoinPoint joinPoint) {
System.out.println("After executing " + joinPoint.getSignature().getName());
}
@AfterThrowing(pointcut = "execution(* com.example.myapp.service.*.*(..))", throwing = "ex")
public void logAfterThrowing(JoinPoint joinPoint, Exception ex) {
System.out.println("Exception thrown by " + joinPoint.getSignature().getName() + " with message: " + ex.getMessage());
}
}
```
这个切面会在执行 `com.example.myapp.service` 包下的所有方法之前、之后和抛出异常时打印日志。