Spring学习笔记③

AOP详解

1.AOP概念

浅显理解:扩展功能不是通过源代码来实现,而是通过配置或者其他方式实现

              采用了横向抽取机制 取代纵向抽取机制

2.AOP原理

底层是动态代理的方式实现

原理图:

动态代理模式与代理模式

动态代理模式是代理模式的一种具体实现形式,在JDK1.3中引入。

3.AOP操作术语

Jointpoint【连接点】    类内可以被增强的方法

Pointcut  【切入点】   在开发中实际被增强的方法

Advice 【通知/增强】    在开发中对方法扩充的功能的部分

           分为

                 前置通知:在方法之前执行

                 后置通知:在方法之后执行

                 异常通知:在方法发生异常时执行

                 最终通知:在后置通知之后执行

                 环绕通知:在方法之前和之后执行

Aspect【切面】  将 增强 应用在  切入点 的 过程 被称为切面

 Use AOP in Spring

--使用AspectJ实现AOP操作

有两种方式:

       配置文件

       注解方式

步骤:①首先导入AOP相关jar包  aspectJ   spring-aspect

        ②新建配置文件 导入schema

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

      ③使用表达式配置切入点:

            常用的表达式配置方法:

                 execution([访问修饰符,一般写 *][space][package.class.function(..)【..表示就算方法有参数也可以识别】])

                 execution(* package.class.*(..))

                 execution(* *.*(..))

                 execution(* save*(..))[表示对所有开头是save的方法进行增强]

      ④创建增强类和被增强类

//增强类
package tech.youngs.aop;

public class Book {
    public void add()
    {
        System.out.println("add...........");
    }
}


//被增强类
package tech.youngs.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {
    public void before()
    {
        System.out.println("前置..........");
    }
    public void after()
    {
        System.out.println("后置...........");
    }
    public void huanrao(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        System.out.println("方法前............");
        
        proceedingJoinPoint.proceed();
        
        System.out.println("方法后............");
    }
}

    ⑤ 配置文件的书写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 第一步 配置增强类和被增强类的对象 -->
     <bean id="book" class="tech.youngs.aop.Book"></bean>
     <bean id="myBook" class="tech.youngs.aop.MyBook"></bean> 
     
     <!-- 第二步 配置AOP -->
     <aop:config>
          <!-- 配置切入点 -->
          <aop:pointcut expression="execution(* tech.youngs.aop.Book.*(..))" id="bookpc"/>
          <!-- 配置切面 -->
          <aop:aspect ref="myBook">
              <aop:before method="before" pointcut-ref="bookpc"/>
              <aop:after-returning method="after" pointcut-ref="bookpc"/>
              <aop:around method="huanrao" pointcut-ref="bookpc"/>
          </aop:aspect>
     </aop:config>    
</beans>

需要注意的是,通知中比较难配置的是环绕通知(增强),在环绕增强中需要引入一个参数--ProceedingJoinPoint

环绕就是切入点前执行,切入点后也执行,所以说需要在环绕增强中配置执行切入点的方法:proceedingJointPoint.proceed();

 

posted @ 2017-03-13 13:05  Youngs的学习之路  阅读(229)  评论(0编辑  收藏  举报