Spring笔记的最后补充 (Spring的基本内容接触完毕)

面向切面(AOP)

面向切面概述

面向切面编程(Aspect-Oriented Programming,简称AOP)是一种编程范式,它旨在通过将横切关注点(如日志记录、事务管理、安全性等)与业务逻辑分离来提高代码的模块化和可维护性。AOP是面向对象编程(OOP)的一个补充,它允许开发者以声明方式实现关注点,而不是通过在业务逻辑代码中散布大量重复代码。以下是对面向切面编程的详细概述:

核心概念
  1. 切面(Aspect)

    • 切面是一组同时横切多个类的代码,封装成一个单元。这些代码通常与业务逻辑不直接相关,但会跨越多个业务模块或组件。
  2. 连接点(Joinpoint)

    • 连接点是程序执行的特定点,如方法的调用或执行。AOP允许在这些连接点插入切面代码。
  3. 通知(Advice)

    • 通知是切面在特定连接点执行的动作。它定义了切面在何时以及如何执行其逻辑。
  4. 切入点(Pointcut)

    • 切入点用于定义哪些连接点会被切面增强。它是一组满足特定条件的连接点的集合。
  5. 目标对象(Target)

    • 目标对象是被切面增强的对象,即业务逻辑代码所在的对象。
  6. 代理(Proxy)

    • 代理是由AOP框架创建的对象,它包含了目标对象的所有方法,并在这些方法调用时加入切面的逻辑。
特点
  1. 模块化

    • AOP通过将横切关注点封装在独立的切面中,实现了代码的高内聚和低耦合。
  2. 动态代理

    • AOP通常通过动态代理技术,在运行时动态地将切面应用到目标对象上,无需修改源代码。
  3. 透明性

    • 切面的应用对业务逻辑代码是无侵入的,业务逻辑代码并不知道切面的存在。
优点
  1. 提高代码的可维护性

    • 将横切关注点分离到独立的切面中,使得业务逻辑代码更加简洁、清晰。
  2. 提高代码的可重用性

    • 切面中的横切关注点逻辑可以在多个模块中重复使用。
  3. 提高系统的灵活性

    • 开发者可以根据实际需求,动态地添加或移除切面,调整系统的功能。
应用场景
  1. 日志记录

    • 在不修改业务逻辑代码的情况下,实现对函数调用的日志记录。
  2. 事务管理

    • 在数据库操作中,使用AOP实现事务的统一管理,确保数据的一致性和完整性。
  3. 安全检查

    • 在业务逻辑代码执行前后,动态地添加安全检查逻辑,确保系统的安全性。
  4. 性能监控

    • 在多个业务模块中统一管理性能数据的收集和分析。
实现方式

AOP可以通过多种方式实现,包括但不限于:

  • 基于代理的AOP:如Spring AOP,它主要基于代理机制,支持基于接口的代理和基于类的代理。
  • 基于编译的AOP:在编译时将切面代码织入到目标代码中。
  • 基于加载时的AOP:在类加载时将切面代码织入到目标类中。
总结

面向切面编程(AOP)是一种强大的编程范式,它通过将横切关注点与业务逻辑分离,提高了代码的模块化和可维护性。在实际项目中,合理地运用AOP可以帮助开发者更好地管理和维护代码,提高软件的质量和开发效率。然而,AOP也存在一定的学习成本和调试难度,需要开发者在实际应用中权衡利弊。

切面的实现方式
第一步:导入切面相关坐标
<!--    切面-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.7</version>
    </dependency>
第二步:在核心配置文件中配置切面的命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
       ">
    
    
    <!--    开启切面注释驱动-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
XML方式
第三步:创建普通类,声明常用方法
package com.xszx.aop;

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


public class  MyAspect02 {


    public void test01() {
        System.out.println("切入指定方法..之前");
    }

    public void test02() {
        System.out.println("切入指定方法。。。之后");
    }

    public void test04() {
        System.out.println("切入指定方法。。。抛异常");
    }

    public void test05() {
        System.out.println("切入指定方法。。。最终通知");
    }
}

第四步:在核心配置文件中,配置切面
<!--    xml开启注解-->
    <bean class="com.xszx.aop.MyAspect02" id="myAspect02"></bean>
    <aop:config>
        <aop:aspect ref="myAspect02" id="myAspect">
            <aop:before method="test01"
                        pointcut="execution(public void com.xszx.controller.UserController.*())"></aop:before>
        </aop:aspect>
    </aop:config>
注解方式
第三步:开启切面注解驱动
<!--        开启切面注释驱动-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
第四步:创建切面类,配置切面注解,配置通知类型
package com.xszx.aop;

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

@Aspect
@Component
public class MyAspect {

    @Pointcut("execution(public void com.xszx.controller.UserController.*())")
    public void point(){

    }

    @Before("point()")
    public void test01() {
        System.out.println("切入指定方法..之前");
    }

    @AfterReturning("point()")
    public void test02() {
        System.out.println("切入指定方法。。。之后");
    }
    @AfterThrowing("point()")
    public void test04() {
        System.out.println("切入指定方法。。。抛异常");
    }
    @After("point()")
    public void test05() {
        System.out.println("切入指定方法。。。最终通知");
    }
    @Around("execution(public void com.xszx.controller.UserController.aopTest())")
    public void test03( ProceedingJoinPoint point) throws Throwable {
        System.out.println("切入指定方法.环绕之前");

        point.proceed();


        System.out.println("切入指定方法.环绕之后");
    }
}

切入点表达式

1.访问修饰符:一般是public

2.返回值:

  • void:没有返回值
  • int: int 类型返回值(基本数据类型和引用数据类型–全限定类名com.beans.user)
  • *:任意返回值

3.包名:

  • com.xszx.service.impl.* : 当前包下的任意类
  • com.xszx.service.*: 当前包下的任意接口
  • com.xszx.service…*:当前包下任意接口以及子包中的任意类

4.类名:

  • UserController:当前类
  • *Controller: 以Controller结尾的类
  • User*:以User开头的类
  • *:任意类名

5.方法名:

  • addUser():当前的方法
  • *User: 以User结尾的方法
  • add*:以add开头的方法
  • *:任意方法名

6.参数名:

  • ():无参
  • (int):参数为一个int类型的参数
  • (Java.long.String):参数为string类型
  • (int,Java.long.String):多个参数
  • (int,*):int和任意的参数
  • (* , *):任意的两个参数
  • (…):任意类型任意个数参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值