在 Spring 中启用 AspectJ 注解支持
1、要在 Spring 应用中使用 AspectJ 注解, 必须在 classpath 下包含 AspectJ 类库: aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
2、将 aop Schema 添加到 <beans> 根元素中.要在 Spring IOC 容器中启用 AspectJ 注解支持, 只要在 Bean 配置文件中定义一个空的 XML 元素 <aop:aspectj-autoproxy>
3、当 Spring IOC 容器侦测到 Bean 配置文件中的 <aop:aspectj-autoproxy> 元素时, 会自动为与 AspectJ 切面匹配的 Bean 创建代理.
1、SpringAOP步骤:
(1).加入jar包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
(2)在配置文件中加入aop的命名空间
(3)基于注解的方式
1、在配置文件中加入如下配置:
<!-- 使AspectJ 注解起作用: 自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、把横切关注点的代码抽象到切面的类中。
i、切面首先是一个IOC容器中的bean,即加入@Component 注解
ii、切面还需要加入@Aspect 注解
3、在类中声明各种通知:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后(可以访问到异常对象,且可以指定在出现特定异常是在执行通知代码)
@Around: 环绕通知, 围绕着方法执行(类似于动态代理的全过程:需要携带ProceedingJoinPoint类型的参数,该参数可以决定是否执行目标方法。且环绕通知必须有返回值,返回值即为目标方法的返回值)
具体是:
i、声明一个方法
ii、在方法前加入@Before注解
最典型的切入点表达式时根据方法的签名来匹配各种方法:
1、execution * com.atguigu.spring.ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 中声明的所有方法,第一个 * 代表任意修饰符及任意返回值. 第二个 * 代表任意方法. .. 匹配任意数量的参数. 若目标类与接口与该切面在同一个包中, 可以省略包名.
2、execution public * ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 接口的所有公有方法.
3、execution public double ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 中返回 double 类型数值的方法
4、execution public double ArithmeticCalculator.*(double, ..): 匹配第一个参数为 double 类型的方法, .. 匹配任意数量任意类型的参数
5、execution public double ArithmeticCalculator.*(double, double): 匹配参数类型为 double, double 类型的方法.
(4)可以在通知方法中声明一个类型为JoinPoint 的参数,然后就能访问链接细节,如方法名称和参数值。
下面是一个前置通知的实例:
//把这个类声明一个切面:首先需要把该类放入到IOC容器中,然后声明一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(public double com.wgs.spring.aop.impl.ArithmeticCalculatorIml.*(double, double))")
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);
}
}
后置通知:和前置通知类似:
其具体代码如下:
import java.util.List;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//把这个类声明一个切面:首先需要把该类放入到IOC容器中,然后声明一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(* com.wgs.spring.aop.impl.*.*(double, double))")
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);
}
//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中,还不能方法目标方法执行的结果。
@After("execution(* com.wgs.spring.aop.impl.*.*(double,double))")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
//List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" ends");
}
}
配置文件为:ApplicationContext.xml
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.wgs.spring.aop.impl"></context:component-scan>
<!-- 使AspectJ 注解起作用: 自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
异常通知
1、 只在连接点抛出异常时才执行异常通知
2、将 throwing 属性添加到 @AfterThrowing 注解中, 也可以访问连接点抛出的异常. Throwable 是所有错误和异常类的超类. 所以在异常通知方法可以捕获到任何错误和异常.
3、如果只对某种特殊的异常类型感兴趣, 可以将参数声明为其他异常的参数类型. 然后通知就只在抛出这个类型及其子类的异常时才被执行.
环绕通知
1、环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.
2、对于环绕通知来说, 连接点的参数类型必须是 ProceedingJoinPoint . 它是 JoinPoint 的子接口, 允许控制何时执行, 是否执行连接点.
3、在环绕通知中需要明确调用 ProceedingJoinPoint 的 proceed() 方法来执行被代理的方法. 如果忘记这样做就会导致通知被执行了, 但目标方法没有被执行.
4、注意: 环绕通知的方法需要返回目标方法执行之后的结果, 即调用 joinPoint.proceed(); 的返回值, 否则会出现空指针异常
环绕通知示例代码
指定切面的优先级
1、在同一个连接点上应用不止一个切面时, 除非明确指定, 否则它们的优先级是不确定的.
2、切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定.
3、实现 Ordered 接口, getOrder() 方法的返回值越小, 优先级越高.
4、若使用 @Order 注解, 序号出现在注解中