AOP概述

  • Aspect Oriented Programming :面向切面编程,AOP是OOP(面向对象编程)的延续,AOP采取横向抽取机制,可以对业务逻辑的各个部分进行隔离,使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,提高开发效率。

AOP实现原理:采用代理机制进行实现

  • 1.   jdk动态代理Proxy     实现  + 类接口 
  • 2.   cglib字节码增强        实现类

AOP术语

  • 1.target:目标类,需要被代理的类。例如:UserService
  • 2.Joinpoint(连接点):代理类中可能被拦截到的方法
  • 3.PointCut:切入点:已经被增强的连接点。目标类中被代理的方法
  • 4.Advice :通知/增强,增强的代码。例如:after、before
  • 5.Weaving:(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.6.
  • 6.proxy:代理类
  • 7.Aspect(切面): 是切入点pointcut和通知advice的结合组成一个特殊的面。

javaaop有什么用_AOP

 AOP联盟通知类型

  • AOP联盟为通知Advice定义了org.aopalliance.aop.Advice    增强代码类型

Spring按照通知 Advice  在目标类方法的连接点位置,可以分为5类

•前置通知 org.springframework.aop.MethodBeforeAdvice   

•后置通知 org.springframework.aop.AfterReturningAdvice

•环绕通知 org.aopalliance.intercept.MethodInterceptor

•异常抛出通知 org.springframework.aop.ThrowsAdvice           在方法抛出异常后实施增强

•引介通知 org.springframework.aop.IntroductionInterceptor   在目标类中添加一些新的方法和属性

AOP全自动编程步骤

  • 1.在bean中配置aop约束
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p ="http://www.springframework.org/schema/p"
       xmlns:context ="http://www.springframework.org/schema/context"
       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/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
</bean>

 2.配置aop:conifg内容,把 切入点 和 通知 Advice 结合

<!--  配置切面类对象-->
    <bean id="myAspect切面类对象名" class="com.zjc.aspect.MyAspect切面类"></bean>

 

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

	<aop:pointcut id="myPointcut" expression="execution(* com.zjc.service.*.*(..))"/>
	<aop:advisor advice-ref="myAspect切面类对象名" pointcut-ref="myPointcut"></aop:advisor>

</aop:config>
  • proxy-target-class:  使用cglib实现代理
  • expression 表达式:  *  任意
  • 语法:execution(修饰符  返回值  包.类.方法名(参数) throws异常)
       例如 execution(     *         com.zjc.service.    *    .      *          (..)     )
                                  返回值    包名                     类名    方法名    参数

AspectJ 通知类型

  • before:前置通知(应用:各种校验)    在方法执行前执行,如果通知抛出异常,阻止方法运行
  • afterReturning:后置通知(应用:常规数据处理)方法正常返回后执行,如果方法中抛出异常,通知无法执行必须在方法执行后才执行,所以可以获得方法的返回值。
  • around:环绕通知(应用:十分强大,可以做任何事情)方法执行前后分别执行,可以阻止方法的执行必须手动执行目标方法
  • afterThrowing:抛出异常通知(应用:包装异常信息)方法抛出异常后执行,如果方法没有抛出异常,无法执行
  • after:最终通知(应用:清理现场)方法执行完毕后执行,无论方法中是否出现异常

AspectJ注解总结

  • @Aspect  声明切面,修饰切面类,从而获得 通知。
  • @Before 前置
  • @AfterReturning 后置
  • @Around 环绕
  • @AfterThrowing 抛出异常
  • @After 最终
  • @PointCut 切入点修饰方法 private void xxx(){}  之后通过“方法名”获得切入点引用

。。。。。。。。。。。待学习