切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
通知(Advice): 切面必需要完成的工做
目标(Target): 被通知的对象
代理(Proxy): 向目标对象应用通知以后建立的对象
链接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。链接点由两个信息肯定:方法表示的程序执行点;相对点表示的方位。例如 ArithmethicCalculator#add() 方法执行前的链接点,执行点为 ArithmethicCalculator#add(); 方位为该方法执行前的位置
切点(pointcut):每一个类都拥有多个链接点:例如 ArithmethicCalculator 的全部方法实际上都是链接点,即链接点是程序类中客观存在的事务。AOP 经过切点定位到特定的链接点。类比:链接点至关于数据库中的记录,切点至关于查询条件。切点和链接点不是一对一的关系,一个切点匹配多个链接点,切点经过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法做为链接点的查询条件。java
编写SpringAOP spring
1、基于注解方式配置AOP数据库
基于 AspectJ 注解或基于 XML 配置的 AOPexpress
1.加入jar包模块化
2.在配置文件中加入AOP的命名空间spa
3.基于注解的方式3d
AspectJ 支持 5 种类型的通知注解:
@Before: 前置通知, 在方法执行以前执行
@After: 后置通知, 在方法执行以后执行 不管该方法是否异常
@AfterRunning: 返回通知, 在方法返回结果以后执行
@AfterThrowing: 异常通知, 在方法抛出异常以后
@Around: 环绕通知, 围绕着方法执行代理
i 声明一个方法日志
ii 在方法前加入@Before注解xml
④访问当前链接点的细节
能够在通知方法中声明一个类型为 JoinPoint 的参数. 而后就能访问连接细节. 如方法名称和参数值.
//日志切面 //把这个类声明为一个切面:须要把该类放入IOC容器中 @Component //再声明为一个切面 @Aspect public class LoggingAspect { //声明该方法是一个前置通知:在目标执行以前执行 @Before("execution(public int aop.ArithmeticCalculator.*(int, int))") public void befordMethod(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(public int aop.ArithmeticCalculator.*(int, int))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends"); }
返回通知
//在方法正常结束时执行的代码,返回通知是能够访问到返回值的 @AfterReturning(value="execution(public int aop.ArithmeticCalculator.*(..))", returning="result") public void afterReturning(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); }
异常通知
//目标方法出现异常时执行的代码,能够访问到异常对象,且能够指定在出现异常时再执行通知代码 @AfterThrowing(value="execution(public int aop.ArithmeticCalculator.*(..))", throwing="ex") public void afterThrowing(JoinPoint joinPoint, Exception ex){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs with: " + ex); }
环绕通知
/** * 环绕通知需携带ProceedingJoinPoint类型的参数 * 环绕通知相似于动态代理的全过程:ProceedingJoinPoint类型的参数能够决定是否执行目标方法 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 * */ @Around("execution(public int aop.ArithmeticCalculator.*(..))") 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 ends with " + result); } catch (Throwable throwable) { throwable.printStackTrace(); //异常通知 System.out.println("The method occurs with exception " + throwable); } //后置通知 System.out.println("---The method " + methodName + " ends"); return result; }
切面的优先级:
使用@Order注解指定切面的优先级,值越小 优先级越高
重用切面表达式 @Pointcut
/** * 定义一个方法,用于声明切入点表达式,通常,该方法不须要传入其余代码 * 使用@Pointcut来声明一个切入点表达式 * 后面的通知直接使用方法名来引用当前的切入点表达式 */ @Pointcut("execution(public int aop.ArithmeticCalculator.*(..))") public void declareJoinPointExpression(){ } //声明该方法是一个前置通知:在目标执行以前执行 @Before("declareJoinPointExpression()") public void befordMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with: " + args); }
2、基于XML配置文件配置AOP
在文件中的注解所有去掉,直接在xml配置文件中进行指定
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置bean--> <bean id="arithmeticCalculator" class="aop.ArithmeticCalculatorImp"></bean> <!--配置切面的bean--> <bean id="loggingAspect" class="aop.LoggingAspect"></bean> <bean id="validationAspect" class="aop.ValidationAspect"></bean> <!--配置AOP--> <aop:config> <!--配置切点表达式--> <aop:pointcut expression="execution(* aop.ArithmeticCalculator.*(..))" id="pointcut"/> <!--配置切面通知--> <aop:aspect ref="loggingAspect" order="2"> <aop:before method="befordMethod" pointcut-ref="pointcut"/> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <!--<aop:around method="aroundMethod" pointcut-ref="pointcut"/>--> </aop:aspect> <aop:aspect ref="validationAspect" order="1"> <aop:before method="validate" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>