Spring 第三篇(AOP)

Spring中的AOP

1. AOP简介

什么是AOP ​

AOP Aspect Oriented Programing 面向切面编程 ​

AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存) ​

SpringAOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码

2.AOP底层原理

AOP底层原理:就是代理机制

动态代理: ​

特点:字节码随用随创建,随用随加载 ​

作用:不修改源码的基础上对方法增强 分类: ​ 基于接口的动态代理 ​ 基于子类的动态代理

Spring的AOP代理: ​

JDK动态代理:被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术。 ​

CGLib动态代理:第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.

结论:Spring框架,如果类实现了接口,就使用JDK的动态代理生成代理对象,如果这个类没有实现任何接口,使用CGLIB生成代理对象

3.AOP的术语

AOP的术语:

​ Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. ​

Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.

​ Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能) ​

Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field. ​

Target(目标对象):代理的目标对象 ​ Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程,spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入 ​

Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类 ​ Aspect(切面): 是切入点和通知(引介)的结合

4.AOP案例1(使用xml文件配置AOP)

1.在pom.xml文件中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qf</groupId>
    <artifactId>spring-13</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
</project>

2.创建通知类

前置通知(before):目标方法运行之前调用

后置通知(after-returning):在目标方法运行之后调用 (如果出现异常不会调用)

环绕通知(around):在目标方法之前和之后都调用(ProceedingJoinPoint对象 -->> 调用proceed方法)

异常拦截通知(after-throwing):如果出现异常,就会调用

最终通知(after):在目标方法运行之后调用 (无论是否出现 异常都会调用)

package com.qf.advice;

import org.aspectj.lang.ProceedingJoinPoint;
//通知Advice
public class MyAdvice {

    public void before(){
        System.out.println("前置通知,目标对象调用方法前执行");
    }

    public void after(){
        System.out.println("后置通知(最终通知),目标对象调用方法后执行,无论是否会发生异常都会执行");
    }

    public void after_Returning(){
        System.out.println("后置通知,目标对象调用方法后执行,发生异常不执行");
    }

    public void after_throwing(){
        System.out.println("异常通知,发生异常时执行");
    }

    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知,目标对象调用方法之前执行");
        joinPoint.proceed();
        System.out.println("环绕通知,目标对象调用方法之后执行");
    }
}

3.创建目标接口及实现类

package com.qf.service;

//目标类target
public interface UserService {
    //未增强的方法称为连接点joinPoint
    //已增强的方法称为切入点PointCut
    void add();

    void delete();
}
package com.qf.service.impl;

import com.qf.service.UserService;

public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("添加方法");
    }

    @Override
    public void delete() {
        System.out.println("删除方法");
    }
}

4.创建applicationContext.xml,添加aop约束

<?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 definitions here -->

    <!--创建目标类对象target-->
    <bean id="userService" class="com.qf.service.impl.UserServiceImpl"></bean>
    <!--创建通知类Advice-->
    <bean id="myAdvice" class="com.qf.advice.MyAdvice"></bean>
    
    <!--aop-->
    <aop:config>
        <!--切点-->
        <aop:pointcut id="pc" expression="execution(public void com.qf.service.UserService.add(..))"/>
        <!--切面-->
        <aop:aspect ref="myAdvice">
            <aop:after method="after" pointcut-ref="pc"></aop:after>
            <aop:before method="before" pointcut-ref="pc"></aop:before>
            <aop:after-returning method="after_Returning" pointcut-ref="pc"></aop:after-returning>
            <aop:after-throwing method="after_throwing" pointcut-ref="pc"></aop:after-throwing>
            <aop:around method="around" pointcut-ref="pc"></aop:around>
        </aop:aspect>
    </aop:config>
     <!-- 配置切入点 切入点表达式的写法:execution(表达式)
   public void com.abyg.service.UserServiceImpl.save() 
   void com.qf.service.UserServiceImpl.save()  其他修饰符无返回值的save空参方法
   * com.qf.service.UserServiceImpl.save()  有或者无返回值的save空参方法
   * com.qf.service.UserServiceImpl.*()  有或者无返回值的所有空参方法
​
   * com.qf.service.*ServiceImpl.*(..)  有或者无返回值的所有有参或者空参方法
   * com.qf.service..*ServiceImpl.*(..)  一般不用,service包下的子包和孙包以ServiceImpl结尾的类中的方法
  -->


</beans
 

5.编写测试类进行测试:

import com.qf.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService =
                (UserService) applicationContext.getBean("userService");
        userService.add();

    }
}

5.Spring中的注解配置AOP(了解会用)

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {
    //自己设置一个切点,管理重复代码
    @Pointcut("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    @AfterReturning("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    @Around("execution(* com.qf.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    @AfterThrowing("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    @After("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}

AOP注解方式下配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
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-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
​
<!-- 准备工作: 导入aop(约束)命名空间 -->
    <!-- 1.配置目标对象 -->
    <bean name="userService" class="com.qf.service.UserServiceImpl" ></bean>
    <!-- 2.配置通知对象 -->
    <bean name="myAdvice" class="com.qf.annotation_aop.MyAdvice" ></bean>
    <!-- 3.开启使用注解完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
​
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小yu别错过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值