Spring笔记03_AOP

2019-04-20  本文已影响0人  itzhouq的笔记

1. AOP

1.1 AOP介绍

1.1.1 什么是AOP

1.1.2 AOP实现原理

1.1.3 AOP术语【掌握】

  1. Target :目标类,需要被代理的类。本例中如:UserDao
  2. Joinpoint(连接点) :所谓连接点是指那些可能被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。本例中如:UserDao的所有的方法
  3. PointCut 切入点 :所谓切入点是指我们要对哪些Joinpoint进行拦截,即已经被增强的连接点。例如:save()
  4. Advice :通知/增强,增强的代码。例如:checkPri()
    所谓通知是指拦截到Joinpoint之后所要做的事情就是通知,通知分为前置通知、后置通知、异常通知、最终通知、环绕通知(即切面要完成的功能)。
  5. Weaving(织入) :是指把通知/增强advice应用到目标对象target来创建新的代理对象proxy的过程。
    spring采用动态代理织入,而AspectJ采用编译期织入和类装在期织入。
  6. Proxy :代理类,一个类被AOP织入增强后,就产生一个结果代理类。
  7. Aspect(切面) : 是切入点Pointcut和通知Advice(引介)的结合。
  8. Introduction(引介) :引介是一种特殊的通知,在不修改类代码的前提下,Introduction 可以在运行期为类动态地添加一些方法或Field。
AOP相关术语

详解如图02:

1.2 AOP的底层实现(了解)

1.2.1 JDK动态代理

1.2.2 Cglib动态代理

1.2.3 总结

2. Spring的AOP的开发(AspectJ的XML方式)

2.1 Spring的AOP简介

2.2 AOP入门开发

2.2.1 准备工程和jar包

2.2.2 引入Spring的配置文件

<?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">
</beans>

2.2.3 编写目标类并完成配置

2.2.4 测试类

package com.itzhouq.spring.demo3;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
 * AOP入门
 */
@RunWith(SpringJUnit4ClassRunner.class)   //这两个注释就是Spring整合了JUnit单元测试
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
  
  //注入productDao
  @Resource(name="productDao")
  private ProductDao productDao;
  
  @Test
  public void test1() {
      productDao.save();
      productDao.update();
      productDao.find();
      productDao.delete();
//        保存商品
//        更新商品
//        查找商品
//        删除商品
  }
}

2.2.5 编写一个切面类

2.2.6 将切面类交给Spring

2.2.7 通过AOP的配置实现动态代理

<!-- 通过AOP的配置完成对目标类产生代理 -->
    <aop:config>
        <!-- 表达式配置哪些类的哪些方法需要进行增强 -->
        <aop:pointcut expression="execution(* com.itzhouq.spring.demo3.ProductDaoImpl.save(..))" id="pointcut1"/>
        <!-- 配置切面 -->
        <aop:aspect ref="myAspect">
            <aop:before method="checkPri" pointcut-ref="pointcust1"/>
        </aop:aspect>
    </aop:config>

2.2.8 测试类中运行结果,save实现了增强

@RunWith(SpringJUnit4ClassRunner.class) //这两个注释就是Spring整合了JUnit单元测试
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
    
    //注入productDao
    @Resource(name="productDao")
    private ProductDao productDao;
    
    @Test
    public void test1() {
        productDao.save();
        productDao.update();
        productDao.find();
        productDao.delete();
//      权限校验。。。
//      保存商品
//      更新商品
//      查找商品
//      删除商品

    }
}

2.3 Spring中的通知类型

2.3.1 前置通知

2.3.2 后置通知:

2.3.3 环绕通知:

2.3.4 异常抛出通知:

2.3.5 最终通知:

2.3.6 引介通知(不用会)

2.4 切入点表达式【掌握】

1.execution()  用于描述方法【掌握】
    语法:execution(修饰符  返回值  包.类.方法名(参数) throws异常)
        修饰符,一般省略
            public      公共方法
            *           任意
        返回值,不能省略
            void        返回没有值
            String      返回值字符串
            *           任意
        包,[可以省略]
            com.itheima.crm                 固定的包
            com.itheima.crm.*.service       crm包下面的任意子包,固定目录service(例如:com.itheima.crm.staff.service)
            com.itheima.crm..               crm包下面的所有子包(含自己)
            com.itheima.crm.*.service..     crm包下面的任意子包,固定目录service,service目录任意包(含自己)
        类,[可以省略]
            UserServiceImpl                 指定的类
            *Impl                           以Impl结尾的类
            User*                           以User开头的类
            *                               任意的类
        方法名,不能省略
            addUser                         固定的方法名
            add*                            以add开头的方法名
            *Do                             以Do结尾的方法名
            *                               任意的方法名
        (参数)
            ()                              无参
            (int)                           一个整型
            (int, int)                      两个整型
            (..)                            参数任意
        throws,[可以省略],一般省略。

    综合案例1:
        execution(* com.itheima.crm.*.service..*.*(..))
    综合案例2:
        <aop:pointcut expression="execution(* com.itheima.*WithCommit.*(..)) || 
                                  execution(* com.itheima.*Service.*(..))" id="myPointCut"/>

2.within:匹配包或子包中的方法(了解)
    within(com.itheima.aop..*)
3.this:匹配实现了接口的代理对象中的方法(了解)
    this(com.itheima.aop.user.UserDAO)
4.target:匹配实现了接口的目标对象中的方法(了解)
    target(com.itheima.aop.user.UserDAO)
5.args:匹配参数格式符合标准的方法(了解)
    args(int, int)
6.bean(id):对指定的bean所有的方法(了解)
    bean('userServiceId')
上一篇 下一篇

猜你喜欢

热点阅读