AOP设计原理-spring-aop

1、为什么要学习AOP设计原理

  • AOP允许我们将通用的流程和代码抽取出来,单独实现,然后给出约定的流程,从而把后续开发者的代码织入约定的流程,从而减少大量重复的工作,使得开发者的工作更为简单,这样业务逻辑就更清晰,代码工作量就更少,尤其是我们核心内容——数据库事务更是如此。
  • 是spirng 的一个重点思想。

2、什么是AOP?

  • 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
  • 主要功能
    日志记录,性能统计,安全控制,事务处理,异常处理等等。
  • 主要意图
    将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。

3、Aop在Spring中的作用

  • 提供声明式事务
  • 允许用户自定义切面
    在这里插入图片描述

以下名词需要了解下:

横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …

切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。

通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

目标(Target):被通知对象。

代理(Proxy):向目标对象应用通知之后创建的对象。

切入点(PointCut):切面通知 执行的 “地点”的定义。

连接点(JointPoint):与切入点匹配的执行点。

在这里插入图片描述

4、实现AOP的第一种方式(使用Spring的API接口)

【maven导入AOP依赖包】

<!--springAOP的包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

【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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd

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

">

- service业务层

public interface UserService {
    void add();
    void delete();
    void query();
    void update();
}

- 业务层的实现类

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加");
    }
    @Override
    public void delete() {
        System.out.println("删除");
    }
    @Override
    public void query() {
        System.out.println("查询");
    }
    @Override
    public void update() {
        System.out.println("修改");
    }
}

3、日志类,在方法前后切入

public class BeforeLog implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}
public class AfterLog implements AfterReturningAdvice {

    /**
     * @param returnValue 方法返回的值
     * @param method      要执行的目标的方法
     * @param args        参数
     * @param target      目标对象
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行" + method.getName() + "返回" + returnValue);
    }
}

配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

  <!--注册bean-->
    <bean id="userService" class="org.lanqiao.service.UserServiceImpl"/>
    <bean id="afterLog" class="org.lanqiao.log.AfterLog"/>
    <bean id="beforeLog" class="org.lanqiao.log.BeforeLog"/>

    <!--方式一:使用原生Spring API接口-->
    <!--配置AOP 需要导入aop的约束-->
    <aop:config>
        <!--切入点 expression表达式:execution(要执行的位置-修饰词,返回值,类名,方法名,参数)-->
        <aop:pointcut id="pointcut" expression="execution(* org.lanqiao.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增强-->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
 </beans>

测试类以及结果:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userservice = context.getBean("userService", UserService.class);
        userservice.add();
    }
}

在这里插入图片描述

5、实现AOP的第二种方式(自定义类)

  • 创建自定义类
public class Diy {
    public void before(){
        System.out.println("执行方法前------------------");
    }
    public void after(){
        System.out.println("执行方法后------------------");
    }
}

  • 配置文件
<!--方式二:自定义类-->
    <bean id="diy" class="org.lanqiao.diy.Diy"/>
    <aop:config>
        <!--自定义切面-->
        <aop:aspect ref="diy">
            <!--切入点 expression表达式:execution(要执行的位置)-->
            <aop:pointcut id="pointcut" expression="execution(* org.lanqiao.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

测试类不变,结果为
在这里插入图片描述

实现AOP的第三种方式(注解)

创建一个类,注解实现

//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
@Component
public class Annotation {
    @Before("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("执行方法前------------------");
    }
    @After("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("执行方法后------------------");
    }
    @Around("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");
        Object proceed = joinPoint.proceed();
      //  Signature signature = joinPoint.getSignature();//获得签名
      //  System.out.println("signature"+signature);
        System.out.println("环绕后");
    }

}

配置文件

 <!--方式三:-->
<!--<bean id="annotation" class="org.lanqiao.diy.Annotation" />-->
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
       <!--扫描包,说明在这个包下的注解会被识别-->
        <context:component-scan base-package="org.lanqiao.diy"></context:component-scan>
         <!--注解的支持-->
       <context:annotation-config></context:annotation-config>

测试结果
在这里插入图片描述

总结:AOP的三种实现方式:

  1. spring原生API实现,创建一个类,实现对应的接口即可。然后在配置文件中配置AOP的切入点和通知(即方法)。当然别忘记了注册bean.
  2. 自定义类,这个对比于第一个就简单的理解一些,自己定义一个类作为切面,里面的方法作为通知。
  3. 使用注解实现,其实类比来看和方式二是一样的,只不过是用注解的方式实现了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏末微风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值