你知道面试必问的AOP吗?通过Spring又如何实现呢?

114 阅读5分钟

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O42ShBjb-1601196297207)(imgkr.cn-bj.ufileos.com/50416f44-5b…)] Aspect Oriented Programing 面向切面编程,相比较 oop 面向对象编程来说,Aop 关注的不再是程序代码中某个类,某些方法,而 aop 考虑的更多的是一种面到面的切入,即层与层之间的一种切入,所以称之为切面。联想大家吃的汉堡(中间夹肉)。那么 aop 是怎么做到拦截整个面的功能呢?考虑学到的 servlet urlpattern /* 的配置,实际上也是 aop 的实现 。 ## Spring Aop 实现的方式 - 注解 方式 - XML 方式 ## 案例实操 ### 注解方式 #### jar 包坐标引入 xml <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> #### beans.xml 配置 添加命名空间 ~~~ xml xmlns:aop="www.springframework.org/schema/aop" ~~~ ~~~ xml www.springframework.org/schema/aop www.springframework.org/schema/aop/… ~~~ 配置 Aop 代理 ~~~ xml aop:aspectj-autoproxy/ ~~~ #### 编写 aop 实现类 ~~~ java /** * 声明切面组件 / @Component @Aspect public class LogCut { /* * 定义切入点 匹配方法规则定义 * 匹配规则表达式含义 拦截 com.xxx.service 包下 以及子包下 所有类的所有方法 / @Pointcut("execution ( com.xxx.service...(..))") public void cut(){} /** * 声明前置通知 并将通知应用到定义的切入点上 * 目标类方法执行前 执行该通知 / @Before(value="cut()") public void before(){ System.out.println("前置通知....."); } /* * 声明返回通知 并将通知应用到切入点上 * 目标类方法执行完毕执行该通知 / @AfterReturning(value="cut()") public void afterReturning(){ System.out.println("返回通知...."); } /* * 声明最终通知 并将通知应用到切入点上 * 目标类方法执行过程中是否发生异常 均会执行该通知 相当于异常中的 finally / @After(value="cut()") public void after(){ System.out.println("最终通知...."); } /* * 声明异常通知 并将通知应用到切入点上 * 目标类方法执行时发生异常 执行该通知 / @AfterThrowing(value="cut()",throwing="e") public void afterThrowing(Exception e){ System.out.println("异常通知....方法执行异常时执行:"+e); } /* * 声明环绕通知 并将通知应用到切入点上 * 方法执行前后 通过环绕通知定义相应处理 / @Around(value="cut()") public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕前置..."); System.out.println("环绕通知"); System.out.println(pjp.getTarget()+"--"+pjp.getSignature()); Object result=pjp.proceed();//执行目标对象方法 System.out.println("环绕后置..."); return result; } } ~~~ #### Aop 匹配方法规则表达式语言(简要了解) Aop 切入点表达式简介 执行任意公共方法: ~~~ java execution(public (..)) ~~~ 执行任意的 set 方法 ~~~ java execution( set(..)) ~~~ 执行 com.xxx.service 包下任意类的任意方法 ~~~ java execution(* com.xxx.service..(..)) ~~~ 执行 com.xxx.service 包 以及子包下任意类的任意方法 ~~~ java execution(* com.xxx.service...(..)) ~~~ ### xml 方式 #### 配置切面、切入点、通知 ~~~ xml aop:config <aop:aspect ref="logCut"> <aop:pointcut expression="execution (* com.xxx.service...(..))" id="cut"/> <aop:before method="before" pointcut-ref="cut"/> <aop:after-returning method="afterReturning" pointcut-ref="cut"/> <aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="cut"/> <aop:after method="after" pointcut-ref="cut"/> <aop:around method="around" pointcut-ref="cut"/> </aop:aspect> </aop:config> ~~~ #### 定义 bean ~~~ java /** * 声明切面组件 */ @Component public class LogCut { public void before(){ System.out.println("前置通知....."); } public void afterReturning(){ System.out.println("返回通知...."); } public void after(){ System.out.println("最终通知...."); } public void afterThrowing(Exception e){ System.out.println("异常通知....方法执行异常时执行:" + e); } public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕前置..."); System.out.println("环绕通知"); System.out.println(pjp.getTarget()+"--"+pjp.getSignature()); Object result=pjp.proceed(); System.out.println("环绕后置..."); return result; } } ~~~ ## 扩展 ### AOP 的基本概念 #### JoinPoint(连接点)【动态】 被拦截到的每个点,spring 中指被拦截到的每一个方法,spring aop 一个连接点即代表一个方法的执行。 #### Pointcut(切入点)【静态】 对连接点进行拦截的定义(匹配规则定义 规定拦截哪些方法,对哪些方法进行处理),spring 这块有专门的表达式语言定义。 #### Advice(通知){重点} 拦截到每一个连接点即(每一个方法)前后所要做的操作 * 前置通知(前置增强)--before() 执行方法前通知 * 返回通知(返回增强)--afterReturning 方法正常结束返回后的通知 * 异常抛出通知(异常抛出增强)--afetrThrow() * 最终通知 --after 无论方法是否发生异常,均会执行该通知 * 环绕通知 --around 包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行 #### Aspect(切面) 切入点与通知的结合,决定了切面的定义,切入点定义了要拦截哪些类的 哪些方法,通知则定义了拦截方法后要做什么,切面则是横切关注点的抽象,与类相似,类是对物体特征的抽象,切面则是横切关注点抽象。 #### Target(目标对象) 被代理的目标对象 #### Weave(织入) 将切面应用到目标对象并生成代理对象的这个过程即为织入(过程)。 #### Introduction(引入) 执行 #### Aspect(切面) 切入点与通知的结合,决定了切面的定义,切入点定义了要拦截哪些类的 哪些方法,通知则定义了拦截方法后要做什么,切面则是横切关注点的抽象,与类相似,类是对物体特征的抽象,切面则是横切关注点抽象。 #### Target(目标对象) 被代理的目标对象 #### Weave(织入) 将切面应用到目标对象并生成代理对象的这个过程即为织入(过程)。 #### Introduction(引入) 在不修改原有应用程序代码的情况下,在程序运行期为类动态添加方法或者字段的过程称为引入。

需要视频配套文档或更多资料+我们程序员小姐姐v:lezijie007(加好友时备注:b站-LT,不备注拒绝添加哟)