SpringAOP的概述与实现

16 篇文章 0 订阅

目录

SpringAOP的概述

什么是AOP

AOP能干什么

AOP的特点

AOP底层实现

AOP基本概念

连接点

切入点

通知

切面

目标对象

织入

引入

谈谈你对AOP的理解?

SpringAOP的实现

依赖引用

spring.xml配置

注解实现

1.定义切面 设置通知

2.开启aop

3.测试

xml实现

1.写切面类

2.配置aop相关配置

3.测试


SpringAOP的概述

什么是AOP

        Aspect oriented Programing面向切面编程,相比较oop 面向对象编程来说,Aop关注的不再是程序代码中某个类,某些方法,而aop考虑的更多的是一种面到面的切入,即层与层之间的一种切入,所以称之为切面。如:大家吃的汉堡(中间夹肉)。

AOP能干什么

        AOP主要应用于日志记录,性能统计,安全控制,事务处理等方面,实现公共功能性的重复使用。

AOP的特点

  1. 降低模块与模块之间的耦合度,提高业务代码的聚合度(高内聚低耦合)
  2. 提高了代码的复用性
  3. 提高系统的扩展性。(高版本兼容低版本)
  4. 可以在不影响原有的功能基础上添加新的功能

AOP底层实现

        动态代理(JDK与CGLIB)

AOP基本概念

连接点

        被拦截到的每个点,spring中指被拦截到的每一个方法,spring aop一个连接点即代表一个方法的执行。

切入点

        对连接点进行拦截的定义(匹配规则定义规定拦截哪些方法,对哪些方法进行处理),spring有专门的表达式语言定义。
 

通知

        拦截到每一个连接点即(每一个方法)后所要做的操作

  1. 前置通知(前置增强)- before()执行方法前通知
  2. 返回通知(返回增强)- afterReturn方法正常结束返回后的通知
  3. 异常抛出通知(异常抛出增强)- afetrThrow()
  4. 最终通知- after无论方法是否发生异常,均会执行该通知。
  5. 环绕通知– around包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行。

切面

        切入点与通知的结合,决定了切面的定义,切入点定义了要拦截哪些类的哪些方法,通知则定义了拦截过方法后要做什么,切面则是横切关注点的抽象,与类相似,类是对物体特征的抽象,切面则是横切关注点抽象。

目标对象

        被代理的对象

织入

        将切面应用到目标对象并生成代理对象的这个过程即为织入

引入

        在不修改原有应用程序代码的情况下,在程序运行期为类动态添加方法或者字段的过程称为引入

谈谈你对AOP的理解?

  1. Aop是面向切面编程,Aop关注的不再是程序代码中某个类,某些方法,而是层与层之间的一种切入
  2. 应用于日志记录,性能统计,安全控制,事务处理等方面,实现公共功能性的重复使用
  3. 作用:降低模块与模块之间的耦合度,提高业务代码的聚合度,提高了代码的复用性,可以在不影响原有的功能基础上添加新的功能
  4.   动态代理(JDK与CGLIB)实现AOP

SpringAOP的实现

依赖引用

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
</dependency>

spring.xml配置

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd

注解实现

1.定义切面 设置通知

package com.lsf.aspect;



import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 定义切面
 *  定义 切入点 与 通知 的结合
 *
 */

@Component  //将该类交给IOC实例化
@Aspect   //声明此类为切面类
public class LogCut {

/**
 * 定义切入点
 *      通过Pointcut定义规则
 *      @Pointcut("execution(* com.lsf.service.*.*(..))")   com.lsf.service下所有方法
 *      例:
 *      1,拦截所有方法
 *      @Pointcut("execution(* *(..))")
 *
 *      2,拦截所有公共的set方法方法
 *      @Pointcut("execution(public set*(..))")
 *
 *
 *  *
 *  *
  */
    @Pointcut("execution(* com.lsf.service.*.*(..))")
    public void cut(){

    }
    /**
     * 前置通知,引用在切入点
     *  在目标方法调用前
     */


    @Before(value = "cut()")
    public void before(){
        System.out.println("这是一个前面通知");

    }

    /**
     * 返回通知,引用在切入点
     *  在目标方法无异常返回时输出
     */
    @AfterReturning(value = "cut()")
    public void afterReturn(){
        System.out.println("返回通知");
    }

    /**
     * 最终通知,引用在切入点
     *  在目标方法是否异常 ,都输出
     */
    @After(value = "cut()")
    public void after(){
        System.out.println("最终通知");
    }

    /**
     * 异常通知,引用在切入点
     *  在目标方法发生异常时 ,都输出
     */
    @AfterThrowing(value = "cut()",throwing = "e")
    public void afterThrow(Exception e){
        System.out.println("异常通知:异常原因:  ");
    }

    /**
     * 环绕通知,引用在切入点
     *  在目标方法发生异常时 ,都输出
     */
    @Around(value = "cut()")
    public Object around(){
        System.out.println("前置通知");
        Object result = null;
        try {
            System.out.println("返回通知");

        }catch (Exception e){
        e.printStackTrace();
            System.out.println("异常通知");
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }finally {
            System.out.println("最终通知");
        }
       
        return result;
    }


}

2.开启aop

  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3.测试

package com.lsf;


import com.lsf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Starter {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = (UserService) ac.getBean("userService");
        userService.test();
    }



}

 

xml实现

1.写切面类

package com.lsf.aspect;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 定义切面
 *  定义 切入点 与 通知 的结合
 *
 */

@Component  //将该类交给IOC实例化
@Aspect   //声明此类为切面类
public class LogCutxml {

    /**
     * 定义切入点
     */

    public void cut(){

    }
    /**
     * 前置通知,引用在切入点
     *  在目标方法调用前
     */



    public void before(){
        System.out.println("这是一个前面通知");

    }

    /**
     * 返回通知,引用在切入点
     *  在目标方法无异常返回时输出
     */

    public void afterReturn(){
        System.out.println("返回通知");
    }

    /**
     * 最终通知,引用在切入点
     *  在目标方法是否异常 ,都输出
     */

    public void after(){
        System.out.println("最终通知");
    }

    /**
     * 异常通知,引用在切入点
     *  在目标方法发生异常时 ,都输出
     */

    public void afterThrow(Exception e){
        System.out.println("异常通知:异常原因:  ");
    }

    /**
     * 环绕通知,引用在切入点
     *  在目标方法发生异常时 ,都输出
     */

    public Object around(){
        System.out.println("前置通知");
        Object result = null;
        try {
            System.out.println("返回通知");

        }catch (Exception e){
        e.printStackTrace();
            System.out.println("异常通知");
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }finally {
            System.out.println("最终通知");
        }

        return result;
    }


}

2.配置aop相关配置

 <!--aop相关配置-->
        <aop:config>
                <!--  aop切面-->
                <aop:aspect ref="logCutxml">
                        <!--定义aop切入点-->
                        <aop:pointcut id="cut" expression="execution(* com.lsf.service..*.*(..))"/><!--配置前置通知指定前置通知方法名︰并引用切入点定义-->
                        <aop:before method="before" pointcut-ref="cut" />
                        <!--配置返回通知指定返回通知方法名并引用切入点定义-->
                        <aop:after-returning method="afterReturn" pointcut-ref="cut"/><!--配置异常通知指定异常通知方法名并引用切入点定义-->
                        <aop:after-throwing method="afterThrow" throwing="e" pointcut-ref="cut" />
                        <!-- 配置最终通知指定最终通知方法名并引用切入点定义-->
                        <aop:after method="after" pointcut-ref="cut" />
                        <!--配置指定环绕通知方法名并引用切入点定义-->
                        <aop:around method="around" pointcut-ref="cut"/>
        </aop:aspect>
</aop:config>

3.测试

package com.lsf;


import com.lsf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Starter {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = (UserService) ac.getBean("userService");
        userService.test();
    }



}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

韶光不负

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

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

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

打赏作者

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

抵扣说明:

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

余额充值