关于Spring AOP的一些术语
- 切面(Aspect):在Spring AOP中,切面可以使用通用类或者在普通类中以@Aspect 注解(@AspectJ风格)来实现
- 连接点(Joinpoint):在Spring AOP中一个连接点代表一个方法的执行
- 通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括"around"、"before”和"after"等通知。许多AOP框架,包括Spring,都是以拦截器做通知模型, 并维护一个以连接点为中心的拦截器链
- 切入点(Pointcut):定义出一个或一组方法,当执行这些方法时可产生通知,Spring缺省使用AspectJ切入点语法。
前置通知:在目标方法开始之前执行
@Component
@Aspect
public class LoginAspect {
@Before("execution(public int spring.aop.impl.AtithmeticCaculator.*(..))")
public void beforeMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("the method "+methodName+" begins with "+args);
}
}
首先将切面声明为bean,并且加入Aspect注解,Before注解表示前置通知
后置通知:在目标方法执行之后开始(无论是否发生异常)
@Component
@Aspect
public class LoginAspect {
@After("execution(public int spring.aop.impl.AtithmeticCaculator.*(..))")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("the method execute after "+methodName+" with "+args);
}
}
返回通知:在方法执行成功之后执行可获取到方法的返回值
@Component
@Aspect
public class LoginAspect {
@AfterReturning(value="execution(public int spring.aop.impl.AtithmeticCaculator.*(..))",returning="result")
public void afterReturnningMethod(JoinPoint joinPoint ,Object result){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("the method execute success after "+methodName+" with "+result);
}
}
异常通知:在目标方法发生异常后执行,可访问到异常对象
@Component
@Aspect
public class LoginAspect {
@AfterThrowing(value="execution(public int spring.aop.impl.AtithmeticCaculator.*(..))",throwing="ex")
public void afterThrowingMethod(JoinPoint joinPoint ,Exception ex){
String methodName=joinPoint.getSignature().getName();
System.out.println("the method "+methodName+" occurs exception with "+ex);
}
}
环绕通知:必须带返回值
@Component
@Aspect
public class LoginAspect {
@Around("execution(public int spring.aop.impl.AtithmeticCaculator.*(..))")
public Object AroundMethod(ProceedingJoinPoint proceedingJoinPoint){
Object result=null;
String methodName=proceedingJoinPoint.getSignature().getName();
try{
//前置通知
System.out.println("the method "+methodName+" begin with "+Arrays.asList(proceedingJoinPoint.getArgs()));
//执行目标方法
result=proceedingJoinPoint.proceed();
//后置通知
System.out.println("the method"+methodName+"end with "+result);
}catch(Throwable e){ //异常通知 System.out.println("the meyhod occurs exception"+e); } return result; }}
配置文件配置AOP
<context:component-scan base-package="spring.aop.xml"></context:component-scan>
<aop:config>
<aop:pointcut expression="execution(* spring.aop.xml.AtithmeticCaculator.*(..))" id="pointcut"/>
<aop:aspect ref="loginAspect">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturnningMethod" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
</aop:aspect>
</aop:config>