核心概念:
切面(Aspect):一个关注点的模块化,在企业应用编程中,首先需要通过分析抽取出通用的功能,即“切面”。事务、日志、安全性的框架、权限都是切面.
连接点(JointPoint) 表示需要在程序中插入横切关注点的扩展点,Spring只支持方法执行连接点,在AOP中表示为“在哪里干”.
切入点(PointCut) 对相关连接点的描述,即可以认为连接点的集合。Spring支持正则表达式和AspectJ切入点模式,Spring默认使用AspectJ语法,在AOP中表示为“在哪里干的集合”.
通知(Advice):在连接点上执行的行为,通知提供了在AOP中需要在切入点所选择的连接点处进行扩展现有行为的手段:包括前置通知(before advice),后者通知(after advice),环绕通知(around advice).
织入(Weaving):织入是一个过程,是将切面应用到目标对象从而创建出AOP代理对象的过程,织入可以在编译期、类装载期、运行期进行.
引入入(inter-type declaration):也称为内部类型声明,为已有的类添加额外新的字段或方法,Spring允许引入新的接口(必须对应一个实现)到所有被代理对象(目标对象), 在AOP中表示为“干什么(引入什么)”.
目标对象(Target Object):需要被织入横切关注点的对象,即该对象是切入点选择的对象,需要被通知的对象,从而也可称为“被通知对象”;由于Spring AOP 通过代理模式实现,从而这个对象永远是被代理对象,在AOP中表示为“对谁干”.
AOP代理(AOP Proxy):AOP框架使用代理模式创建的对象,从而实现在连接点处插入通知(即应用切面),就是通过代理来对目标对象应用切面。在Spring中,AOP代理可以用JDK动态代理或CGLIB代理实现,而通过拦截器模型应用切面。
2 注解形式在spring boot中运用AOP
2.1 写切面类:
package com.example.aop_rbmq.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.util.Arrays; @Aspect @Component public class TimeStaticsAspect { public static final String POINT="execution (* com.example.aop_rbmq..*.*(..))"; //前置通知:在controller下的每一个实现类的每一个方法开始之前执行一段代码 @Before("execution (* com.example.aop_rbmq.controller.*.*(..))") public void beforeMethod(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = getMethodName(signature); Object[] args = joinPoint.getArgs(); System.out.println("before method " + methodName + " begin with:" + Arrays.asList(args)); } //后置通知:在controller下的每一个实现类的每一个方法执行之后执行的代码. 无论该方法是否出现异常 @After("execution (* com.example.aop_rbmq.controller.*.*(..))") public void afterMethod(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = getMethodName(signature); Object[] args = joinPoint.getArgs(); System.out.println("after method:" + methodName + " end " + Arrays.asList(args) ); }
//返回通知:在方法法正常结束受执行的代码。返回通知是可以访问到方法的返回值的! @AfterReturning(value = POINT, returning = "result") public void afterReturning(JoinPoint joinPoint, Object result) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = getMethodName(signature); Object[] args = joinPoint.getArgs(); System.out.println("The method:" + methodName + " ends with " + result ); } //异常通知:在目标方法出现异常时会执行的代码 @AfterThrowing(value = POINT, throwing = "e") public void afterThrowing(JoinPoint joinPoint, Exception e) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = getMethodName(signature); System.out.println("The method " + methodName + " occurs exception:" + e); }
/** * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法. * 且环绕通知必须有返回值, 返回值即为目标方法的返回值 */ @Around(POINT) public Object timeAround(ProceedingJoinPoint joinPoint) { Object ret = null; Object[] params = joinPoint.getArgs(); long st = System.currentTimeMillis(); try { ret = joinPoint.proceed(params); } catch(Throwable e) { System.out.println("execute TimeStaticsAspect::timeAround failed " + e); } long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = getMethodName(signature); this.printExecTime(methodName, st, endTime); return ret; } private void printExecTime(String methodName, long startTime, long endTime) { long diffTime = endTime - startTime; System.out.println(methodName + " 方法执行耗时:" + diffTime + " ms"); } private String getMethodName(MethodSignature signature) { return signature.getDeclaringTypeName() + "::" + signature.getName(); } }
2.2 controller
package com.example.aop_rbmq.controller; import com.example.aop_rbmq.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Autowired private OrderService service; @GetMapping("/order") public String sendPayment(@RequestParam(required = true, name = "orderid") String orderid) throws Exception { return service.getOrder(orderid); } }
2.2 service
package com.example.aop_rbmq.service; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; @Service public class OrderService { private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); public String getOrder(String orderid) throws Exception{ System.out.println("Time:" + formatter.format(new Date()) + " process " + orderid); Thread.sleep(100); return orderid+ " is successful"; } }
2.3 SpringBootApplication
package com.example.aop_rbmq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //@ComponentScan(basePackages={"com.example.aop_rbmq.aop","com.example.aop_rbmq.controller"}) public class AopRbmqApplication { public static void main(String[] args) { SpringApplication.run(AopRbmqApplication.class, args); } }
2.4 测试
http://127.0.0.1:8080/order?orderid=1
server 输出
before method com.example.aop_rbmq.controller.OrderController::sendPayment begin with:[1]
Time:2019-01-13 16:47:01:281 process 1
com.example.aop_rbmq.service.OrderService::getOrder 方法执行耗时:125 ms
The method:com.example.aop_rbmq.service.OrderService::getOrder ends with 1 is successful
com.example.aop_rbmq.controller.OrderController::sendPayment 方法执行耗时:141 ms
after method:com.example.aop_rbmq.controller.OrderController::sendPayment end [1]
The method:com.example.aop_rbmq.controller.OrderController::sendPayment ends with 1 is successful