Spring--AOP简介

本文深入探讨了面向切面编程(AOP)的概念及其在Spring框架中的应用。通过实例介绍了如何利用Spring AOP进行日志记录,展示了AOP增强业务逻辑模块化、降低耦合性的优势。

AOP:面向切面编程
OOP:面向对象编程

面向切面编程:基于OOP基础之上新的编程思想。
指的是在程序运行期间,将某段代码动态的切入到指定方法的指定位置进行运行的这种编程方式。

利用AOP可以对业务逻辑的各个部分进行隔离,使得业务逻辑各部分之间的耦合性降低,提高程序的可重用性,同时提高了开发效率

动态代理:

  1. 写起来复杂
  2. jdk默认的动态代理,如果目标对象没有实现任何接口,是无法为它创建代理对象的

spring实现了AOP功能,底层就是动态代理

  • 可以利用spring一句代码都不写的去创建动态代理,且没有强制要求目标对象必须实现接口

AOP的一些术语:
以一个计算器类的日志记录为例子:
在这里插入图片描述
在每一个横切关注点上都调用了一个方法进行日志记录,这个方法就是通知方法。
通知方法:在目标方法执行前后执行的方法
而通知方法存在的类就是切面类。

AOP的使用步骤

  1. 导包(pom.xml)
  <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.5</version>
  </dependency>
  1. 写配置
  • 将目标类和切面类(封装通知方法的类)加入IOC容器中。
  • 告诉spring哪个是切面类(加注解@Aspect)
  • 告诉spring切面类中的方法何时何地运行
    在这里插入图片描述
    在这里插入图片描述
  • 开启基于注解的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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


        <context:component-scan base-package="com.jess"></context:component-scan>

        <!-- 开启基于注解的AOP功能: aop名称空间-->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
  1. 测试

简单代码实现:

package com.jess.aop;
public interface Calculator {    
    public int add(int i,int j);
    public int sub(int i,int j);
    public int mul(int i,int j);
    public int div(int i,int j);
}
package com.jess.aop;
import org.springframework.stereotype.Service;

@Service
public class MyCalculator implements Calculator{
    public int add(int i, int j) {
        int result=i+j;
        return result;
    }
    public int sub(int i, int j) {
        int result=i-j;
        return result;
    }
    public int mul(int i, int j) {
        int result=i*j;
        return result;
    }
    public int div(int i, int j) {
        int result=i/j;
        return result;
    }
}

切面类:

package com.jess.aop;

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

@Aspect
@Component
public class LogUtils {

    //在目标方法执行前执行
    //切入点表达式:execution(访问权限服 返回值类型 方法签名)
    @Before("execution(public int com.jess.aop.MyCalculator.*(int,int))")
    public static void logStrat(){
        System.out.println("方法开始执行。。。。");
    }

    //在目标方法正常返回时执行
    @AfterReturning("execution(public int com.jess.aop.MyCalculator.*(int,int))")
    public static void logReturn(){
        System.out.println("方法执行完成。。。。");
    }

    //在目标方法抛出异常时执行
    @AfterThrowing("execution(public int com.jess.aop.MyCalculator.*(int,int))")
    public static void logException(){
        System.out.println("方法出现异常。。。。");
    }

    //在目标方法执行后执行
    @After("execution(public int com.jess.aop.MyCalculator.*(int,int))")
    public static void logEnd(){
        System.out.println("方法结束。。。。");
    }
}

测试类:

public class AOPTest {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("application.xml");

    @Test
    public void test() {
        //从IOC容器中拿到目标对象,如果想要用类型,一定要用接口类型,不要用本类
        Calculator bean = ioc.getBean(Calculator.class);
        bean.add(2,12);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值