前置通知:@Before,在连接点方法执行之前执行。
后置通知:@After,在连接点方法执行之后,无论如何都会执行。
环绕通知:@Around,在连接点方法执行之前和之后执行。连接点发生异常时,之后不执行。
异常通知:@AfterThrowing,在连接点方法发生异常之后执行。
返回通知:@AfterReturning,在连接点方法返回结果后执行。如果发生异常,则不会执行。
1、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
2、业务类
public class UserService {
public int addUser(String name) {
// System.out.println(5/0);
System.out.println("新增用户");
return 1;
}
}
3、切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
class UserAspects {
@Pointcut("execution(public int com.it.huaxin.aop.UserService.addUser(..))")
public void pointCut(){};
@Before("pointCut()")
public void userBefore() {
System.out.println("@Before,前置通知");
}
@After("pointCut()")
public void userAfter () {
System.out.println("@After,后置通知");
}
@Around("pointCut()")
public Object userAround (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("@Around,环绕通知,执行方法之前");
Object proceed = proceedingJoinPoint.proceed(new Object[]{"张三"});
System.out.println("@Around,环绕通知,执行方法之后");
return proceed;
}
@AfterThrowing("pointCut()")
public void userThrowing () {
System.out.println("@AfterThrowing,异常通知,方法发生异常之后执行");
}
@AfterReturning("pointCut()")
public void userReturing() {
System.out.println("@AfterReturning,返回通知 ");
}
}
4、配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class AopConfig {
@Bean
public UserService userService() {
return new UserService();
}
@Bean
public UserAspects userAspects() {
return new UserAspects();
}
}
5、测试类
import com.it.huaxin.aop.AopConfig;
import com.it.huaxin.aop.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AopTest {
public static void main(String [] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
UserService bean = context.getBean(UserService.class);
bean.addUser("张三");
}
}
//结果:
@Around,环绕通知,执行方法之前
@Before,前置通知
新增用户
@Around,环绕通知,执行方法之后
@After,后置通知
@AfterReturning,返回通知
// 发生异常,结果:
@Around,环绕通知,执行方法之前
@Before,前置通知
@After,后置通知
@AfterThrowing,异常通知,方法发生异常之后执行
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.it.huaxin.aop.UserService.addUser(UserService.java:5)
at com.it.huaxin.aop.UserService$$FastClassBySpringCGLIB$$c2371e8f.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:100)
at com.it.huaxin.aop.UserAspects.userAround(UserAspects.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.it.huaxin.aop.UserService$$EnhancerBySpringCGLIB$$fcde3768.addUser(<generated>)
at com.it.huaxin.test.AopTest.main(AopTest.java:11)
本文详细介绍了Spring AOP中的各种通知类型,包括前置通知(@Before),后置通知(@After),环绕通知(@Around),异常通知(@AfterThrowing)和返回通知(@AfterReturning),并通过实例展示了它们在实际业务中的应用场景和执行顺序。
6405

被折叠的 条评论
为什么被折叠?



