美文网首页
Spring入门02--AOP(面向切面编程)

Spring入门02--AOP(面向切面编程)

作者: CodeManLB | 来源:发表于2018-01-11 18:02 被阅读0次

一,理解AOP(Aspect-oriented Programming)

  1. 传统的OOP面向对象编程,引入了继承,封装,多态等概念用来建立对象间的纵向的层次关系。并不适合定义横向的关系,例如日志代码,它分布在所有对象层次中,但是与核心业务却毫无关系,这些散布在各处且和核心业务无关的代码,导致了大量代码的重复;
  2. AOP面向切面编程,它的关注点就是如何封装这些被大量使用,却和核心业务无关的代码称一个个模块,这些模块即是切面;AOP把软件系统分为两个部分:核心关注点(核心业务)和横切关注点(切面);

二,理解Spring AOP的一些关键词:

  1. 切面:封装起来的会被大量复用,且和核心业务无关的代码,通常是一些普通的java类;
  2. 通知:定义了如何去使用这些切面,通常分为:前置,后置,环绕,和异常通知;
  3. 目标对象:将会应用到通知并被代理的对象;
  4. 代理对象:目标对象织入切面后创建的对象;
  5. 切入点:定义对象中的哪些方法将会被拦截并执行切面的代码;
  6. 织入:将切面应用到目标对象并产生代理对象的过程;

三,Spring如何实现AOP

  1. 基于ProxyFactoryBean的AOP实现:
    这里分别提供四种不同的通知接口:MethodBeforeAdvice(前置通知),MethodAfterAdvice(后置通知),ThrowsAdvice(异常通知),MethodInterceptor(环绕通知),设计普通的java类实现接口中的方法,即可编写切面代码即可;然后重新配置一个代理类的bean,将切面织入目标对象,使用这个代理对象即可;这个方法过于麻烦,就不列出来了;
  2. 基于Schema的AOP实现:
    2.1, 定义切面的实现类:
public class AllAdvice
{
  public void myBeforeAdvice(JoinPoint joinPoint)
  {
    System.out.println("前置通知......");
  }
  public void myAfterAdvice(JoinPoint joinPoint)
  {
    System.out.println("后置通知......");
  }
  public void myThrowingAdvice(JoinPoint joinPoint,Exception e)
  {
    System.out.println("异常通知......");
  }
  public void myAroundAdvice(ProceedingJoinPoint joinPoint)
  {
    System.out.println("环绕通知......");
  }
}

      2.2, 在配置文件设置AOP:

  //装配AllAdvice的实例
  <bean id="allAdvice" class="com.spring.advice.AllAdvice"/>
  //进行AOP的配置
  <aop:config>
    <!-- 定义切面 -->
    <aop:aspect id="allAdvice" ref="allAdvice">
      <!-- 定义切入点,切入点采用正则表达式-->
      <aop:pointcut id="show" expression="execution(* com.spring.beans.*(..))" />
      <!--给切入点织入切面-->
      <!-- 指定前置通知 -->
      <aop:before method="myBeforeAdvice" pointcut-ref="show"/>
      <!-- 指定后置通知 -->
      <aop:after-returning method="myAfterAdvice" pointcut-ref="show" throwing="e"/>
      <!-- 指定异常通知 -->
      <aop:after-throwing method="myThrowAdvice" pointcut-ref="show"/>
      <!-- 指定环绕通知 -->
      <aop:around method="myAroundAdvice" pointcut-ref="show"/>
    </aop:aspect>
  </aop:config>
  1. 基于@AspectJ注解的AOP实现:
//定义切面
@Aspect
public class AllAdviceByAspectJ
{
    //定义切入点
    @Pointcut("execution(void com.spring.beans.HelloWorld.show(String))")
    //定义切入点名字
    private void show(){}
    
    @Pointcut("execution(void com.spring.beans.HelloWorld.log(String))")
    private void log(){}
    //前置通知
    @Before("show(),log()")
    public void myBeforeAdvice(JoinPoint joinPoint)
    {
        System.out.println("calling Schema BeforeAdvice......");
        System.out.println(System.currentTimeMillis());
    }
    //后置通知
    @AfterReturning("show()")
    public void myAfterAdvice(JoinPoint joinPoint)
    {
        System.out.println("calling Schema AfterAdvice......");
        System.out.println(System.currentTimeMillis());
    }
    //异常通知
    @AfterThrowing(pointcut="show()",throwing="e")
    public void myThrowsAdvice(JoinPoint joinPoint,Exception e)
    {
        System.out.println("calling Schema ThrowsAdvice......"+e.getMessage());
        System.out.println(System.currentTimeMillis());
    }
    //环绕通知
    @Around("show()")
    public void myInterceptorAdvice(ProceedingJoinPoint joinPoint) throws Throwable
    {
        System.out.println("calling Schema InterceptorAdvice......");
        System.out.println(System.currentTimeMillis());
        joinPoint.proceed();
    }
    
}

相关文章

网友评论

      本文标题:Spring入门02--AOP(面向切面编程)

      本文链接:https://www.haomeiwen.com/subject/slktoxtx.html