spring面向切面(AOP)编程

本文详细介绍了Spring AOP的四种实现方式:原生API、自定义切面、注解通知,并通过实际代码展示了如何在权限验证、日志追踪等场景中应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:AOP概念

AOP即面向切面编程,是面向对象编程(OOP)的一种补充。在OOP中只能实现父子关系的纵向的代码重用,AOP采取横向抽取,将分散在各个方法中的重复代码提取出来通过动态代理应用到各个方法中。

AOP名词理解(不需过分记忆及理解,懂AOP的实现及用法即可):

  • 横切关注点:我们要关注的功能,即横向抽取分散在各个方法中的重复功能,如日志,权限等
  • 切面(Aspect):横切关注点 被完整定义模块,如日志切面,即它是一个类
  • 通知(Advice):切面要完成的功能,如日志功能方法,即它是类中的一个方法
  • 目标对象(Target):被切面切的对象。真正的业务逻辑
  • 代理(proxy):实现AOP的一种原理。对目标对象进行代理功能增强
  • 连接点(Join point):能够植入切面的执行点,比如方法的前后,抛出异常时都可以是连接点
  • 切入点(Pointcut):针对哪些连接点植入通知,也就是指定具体的拦截地点。
  • 引入(Introduction):对目标类添加新方法及属性
  • 织入(Weaving):把切面应用到目标对象来创建新的代理对象的过程。

AOP的5类通知类型:

  1. 前置通知:方法前通知,实现接口MethodBeforeAdvice
  2. 后置通知:方法后通知,实现接口AfterReturningAdvice
  3. 环绕通知:方法前后通知,实现接口MethodInterceptor
  4. 抛出异常后通知:方法抛出异常后通知,实现接口ThrowsAdvice
  5. 引介通知:类中增加新的方法和属性,实现接口IntroductionInterceptor

Aop应用场景:
1.权限验证
2.性能追踪
3.日志追踪
4.异常处理
5.事务 => Spring 的声明式事务管理
6.缓存
7.懒加载

二:AOP实现方式一:使用原生Spring API接口(主要SpringAPI接口实现)

引入面向切面编程的jar包:

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

目标接口:

package com.lmy.service;

/**
 * @author 86152
 */
public interface UserService {
   
   
    /**
     * 增加
     */
    void add();
    /**
     * 删除
     */
    void delete();
    /**
     * 修改
     */
    void update();
    /**
     * 查询
     */
    void select();
}

目标类:

package com.lmy.service.impl;

import com.lmy.service.UserService;

/**
 * @author : liu ming yong
 * @date : 2022/7/23 下午 8:27
 * @description : 增删改查
 */
public class UserServiceImpl implements UserService {
   
   
    public void add() {
   
   
        System.out.println("增加");
    }

    public void delete() {
   
   
        System.out.println("删除");
    }

    public void update() {
   
   
        System.out.println("修改");
    }

    public void select() {
   
   
        System.out.println("查询");
    }
}

配置xml文件:

<?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-->
    <bean id="userService" class="com.lmy.service.impl.UserServiceImpl"/>
    <bean id="beforeLog" class="com.lmy.aop.BeforeLog"/>
    <bean id="afterLog" class="com.lmy.aop.AfterLog"/>

    <!--配置aop:需要导入aop的约束-->
    <aop:config>
        <!--切入点: expression:表达式,execution(要执行切入的目标对象位置 * * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.lmy.service.impl.UserServiceImpl.*(..))"/>

        <!--执行增强-->
        <aop:advisor advice
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值