代码示例
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
@Aspect
@Component
public class HelloAop {
@Pointcut("execution(* com.example.hello.controller.HelloController.hello(..))")
public void pointcut() {
}
@Before("pointcut()")
public void before(JoinPoint joinPoint) {
System.out.println("@Before");
}
@AfterThrowing("pointcut()")
public void afterThrowing() {
System.out.println("@AfterThrowing");
}
@AfterReturning("pointcut()")
public void afterReturning() {
System.out.println("@AfterReturning");
}
@After("pointcut()")
public void after() {
System.out.println("@After");
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object result;
try {
System.out.println("@Around...方法执行前");
result = pjp.proceed();
System.out.println("@Around...方法执行后");
} catch (Throwable throwable) {
System.out.println("@Around...抛出异常后");
throw throwable;
} finally {
System.out.println("@Around...finally");
}
return result;
}
}