Spring中AOP的理解

1.AOP的概念

         AOP(AspectOriented Programming,面向切面编程)指的是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下个程序动态统一添加功能的一种技术。AOP提供从一个角度来考虑程序结构以完善面向对象编程。它为开发人员提供了一种描述横切关注点的机制,并能够自动将横向关注点入到面向对象的软件系统中,从而实现了横切关注点模块化

         AOP一般用于实现那些与业务无关,但是业务模块所共同调用的逻辑,例如异常拦截、事务管理、日志管理和权限控制等,AOP将这些代码封装起来,有利于减少系统的重复代码,降低模块间耦合度,提供可维护性。

         AOP的基本概念:Aspect(切面):一个关注点的模块化,这个关注点可能会横切多个对象;Joinpoint(连接点):程序执行过程中某个特定的点,比如处理异常的时候;Advice(通知):在切面的某个特定连接点上执行的操作;Pointcut(切入点):匹配连接点的断言;Introduction(引入):用来给一个类型声明额外的方法和属性;Target Object(目标对象):被一个或多个切面所统治的对象;Weaving(织入):被切面连接到其他应用程序类型或对象上,并创建被通知的对象。

         2.AOP的实现原理

         AOP的核心实现与IoC一样依赖于Java的反射机制,但是与IoC不同的地方是,IoC用到的相关设计模式是工程模式,而AOP用到的是代理模式。代理模式的思想是不让外部直接访问具体目标,而是需要通过一个代理类才能访问,所有与具体目标的通信都能通过这道“隔离层”来传送请求或返回状态。

         3.AOP的通知类型

/*

         1、Before advice(前置通知):在方法执行之前执行,在一个切面里使用@Before

         注解声明通知。配置参考示例如下:

*/

<bean id="afterReturningAdviceExample" class="完整类名" />

         <aop:config proxy-target-class="false">

                   <!-- 定义切入点 -->

                   <aop:pointcut type="aspectj" id="beforePointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定义切面 -->

                   <aop:aspect ref="beforeAdviceExample">

                            <aop:before pointcut-ref="beforePointcut" method="beforeAspect" />

                   </aop:aspect>

         </aop:config>

/*

         execution(*com.itjob.spring.aop..*Manager.(..))表示切入点为com.itjob.spring.aop

         包及其子包中以Manager结尾的类的任意方法钱作为切入点

*/

/*

         2、After returning advice(返回后通知):通常在已匹配的方法返回的时候执行,

         也可使用@AfterReturning注释来声明。配置如下

*/

<bean id="afterReturningAdviceExample" class="完整类名" />

         <aop:config proxy-target-class="false">

                   <!-- 定义切入点 -->

                   <aop:pointcut type="aspectj" id="afterReturningPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定义切面 -->

                   <aop:aspect ref="afterReturningAdviceExample">

                            <aop:after-returning pointcut-ref="afterReturningPointcut"

                                     method="afterReturningAspect" returning="returnValue"/>

                   </aop:aspect>

         </aop:config>

/*

         3、After throwing advice(抛出后通知):在一个方法抛出异常后执行。采用声明

         方式定义切入点时,可使用@AfterThrowing注释来声明,配置入下

*/

<bean id="afterThrowingAdviceExample" class="完整类名" />

         <aop:config proxy-target-class="false">

                   <!-- 定义切入点 -->

                   <aop:pointcut type="aspectj" id="afterThrowingPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定义切面 -->

                   <aop:aspect ref="afterThrowingAdviceExample">

                            <aop:after-throwing pointcut-ref="afterThrowingPointcut"

                                     method="afterThrowingAspect" throwing="ex"/>

                   </aop:aspect>

         </aop:config>

/*

         4、After (finally)advice(后通知):不论一个方法是如何结束的,在它结束后

         通知都会运行。若使用声明式,可使用@After注释来声明。这个通知必须处理

         正常返回和异常返回两种情况,通常来释放资源,配置如下:

*/

<bean id="afterAdviceExample" class="完整类名" />

         <aop:config proxy-target-class="false">

                   <!-- 定义切入点 -->

                  <aop:pointcut type="aspectj" id="afterPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定义切面 -->

                   <aop:aspect ref="afterAdviceExample">

                            <aop:after pointcut-ref="afterPointcut"

                                     method="afterAspect" throwing="ex"/>

                   </aop:aspect>

         </aop:config>

/*

         5、Around Advice(环绕通知):在一个方法执行之前和之后执行,并且它可以决定

         这个方法在什么时候执行、如何执行、甚至是是否执行。声明式的环绕通知可用

         @Around注释来声明。

*/

<bean id="aroundAdviceExample" class="完整类名" />

         <aop:config proxy-target-class="false">

                   <!-- 定义切入点 -->

                   <aop:pointcut type="aspectj" id="aroundPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定义切面 -->

                   <aop:aspect ref="aroundAdviceExample">

                            <aop:after pointcut-ref="aroundPointcut"

                                     method="aroundAspect" />

                   </aop:aspect>

         </aop:config>

         4.AOP适用场景

         1)系统权限管理;2)错误处理和异常拦截;3)事务管理;4)持久化;5)同步问题;6)系统调试;7)性能优化;8)日志

原文地址:https://www.cnblogs.com/hanfeihanfei/p/6708318.html