AOP操作

本文介绍如何利用Spring框架结合AspectJ实现面向切面编程(AOP),包括配置切入点表达式、不同类型的AOP通知及使用注解进行开发的具体步骤。

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

1.AOP操作(准备)

1.Spring框架一般都是基于AspectJ实现AOP操作
(1)什么是AspectJ
*AspectJ不是Spring组成部分,独立AOP框架,一般AspectJ和Spring框架一起使用,进行AOP操作

2.基于AspectJ实现AOP操作
(1)基于xml配置文件
(2)基于注解方式实现(实用)

3.在项目工程里面引入AOP相关依赖

4.切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构:
execution([权限修饰符] [返回类型] [类全路径]方法名称)

举例1:对BookDao类里面的add进行增强
execution(* xxx.xxx.BookDao.add(…) )

举例2:对BookDao类里面的所有方法进行增强
execution(* xxx.xxx.BookDao.*(…) )

举例3:对包里所有类里面的所有方法进行增强
execution(* xxx.xxx.* . *(…) )

2.AOP操作(AspectJ注解)

1.创建类,在类里面定义方法

//被增强类
public class User {
    public void add(){
        System.out.println("add...");
    }
}

2.创建增强类(编写增强逻辑)
(1)在增强类里面,创建方法,让不同方法代表不同通知类型

//增强类
public class UserProxy {
    //前置通知
    public void before(){

        System.out.println("before..");
    }
}

3.进行通知配置
(1)在spring配置文件中,开启注解扫描

<?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.wyt.spring5.aopanno"></context:component-scan>
</beans>

(2)使用注解创建User和UserProxy对象
在这里插入图片描述
在这里插入图片描述

(3)在增强类上面添加注解@Aspect
在这里插入图片描述

(4)在spring配置文件中开启生成代理对象

<!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4.配置不同类型通知
(1)在增强类里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置


package com.wyt.spring5.aopanno;

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

//增强类
@Component
@Aspect  //生成代理对象
public class UserProxy {
    //前置通知
    //Before注解表示前置通知
    @Before(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void before(){
        System.out.println("before..");
    }

    //最终通知
    @After(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after..");
    }

    //后置通知
    @AfterReturning(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void afterreturning(){
        System.out.println("AfterReturning..");
    }

    //异常通知
    @AfterThrowing(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void AfterThrowing(){
        System.out.println("AfterThrowing..");
    }

    @Around(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前..");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后..");

    }




}

5.公共切入点抽取

 //相同切入点抽取
    @Pointcut(value ="execution(* com.wyt.spring5.aopanno.User.add(..))" )
    public void pointdemo(){

    }

6.有多个增强类对同一个方法进行增强,设置增强优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高

@Component
@Aspect
@Order(3)
public class PersonProxy {
    @Before(value = "execution(* com.wyt.spring5.aopanno.User.add(..))")
    public void before(){
        System.out.println("Person before..");
    }
}

7.完全使用注解开发
(1)创建配置类,不需要创建xml配置文件

@Configuration
@ComponentScan(basePackages ={"com.wyt"} )
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class config {
}

3.AOP操作(AspectJ配置文件)

1.创建两个类,增强类和被增强类,创建方法
2.在spring配置文件中创建两个对象
3.在spring配置文件中配置切入点



<!--    创建对象-->
    <bean id="book" class="com.wyt.spring5.aopxml.Book"></bean>
    <bean id="bookProxy" class="com.wyt.spring5.aopxml.BookProxy"></bean>
    
<!--    配置aop增强-->
    <aop:config>
<!--        切入点-->
        <aop:pointcut id="p" expression="execution(* com.wyt.spring5.aopxml.Book.buy(..))"/>
<!--        配置切面-->
        <aop:aspect ref="bookProxy">
<!--           增强作用在具体的方法上 -->
            <aop:before method="before" pointcut-ref="p"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值