aop切面编程示例代码
时间: 2023-11-20 17:45:34 浏览: 78
AOP面向切面编程实例
aop切面编程的示例代码如下:
```java
public class LoggingAspect {
@Before("execution(* com.example.service.UserService.*(..))")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method: " + joinPoint.getSignature().getName());
}
@After("execution(* com.example.service.UserService.*(..))")
public void afterAdvice(JoinPoint joinPoint) {
System.out.println("After method: " + joinPoint.getSignature().getName());
}
@Around("execution(* com.example.service.UserService.*(..))")
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Before method: " + proceedingJoinPoint.getSignature().getName());
proceedingJoinPoint.proceed();
System.out.println("After method: " + proceedingJoinPoint.getSignature().getName());
}
}
```
这段代码是一个简单的日志切面示例,通过使用AOP技术,我们可以在UserService类的所有方法执行前后打印相应的日志信息。
阅读全文