Spring学习笔记【part08】AOP注解开发

本文介绍了Spring框架中AOP的注解开发方法,包括配置IoC容器支持注解、开启AOP注解支持等步骤,并详细解释了@Aspect、@Before、@AfterReturning、@AfterThrowing、@After、@Pointcut和@Around等注解的使用。

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

Spring 学习笔记 Part08

1. AOP注解开发前的配置

导入注解开发需要的context命名空间。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

配置IoC注入容器的注解,并在java包下配置@Service(“accountService”)、@Repository(“accountDao”)和@Component(“logger”)。

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

开启spring对AOP注解的支持

<aop:aspectj-autoproxy/>

2. 注解标签介绍

@Aspect:表明当前类是一个切面类

@Component("logger")
@Aspect
public class Logger {
    
}

@Before:表明当前方法是一个前置通知

@AfterReturning:表明当前方法是一个后置通知

@AfterThrowing:表明当前方法是一个异常通知

@After:表明当前方法是一个最终通知

以上四个的共有属性:

​ value:用于指定切入点表达式(execution关键字),或者指定切入点表达式的引用(配合@Pointcut注解)。

我们会在开发中发现@AfterReturning和@After两个通知的顺序可能会乱,因此在注解开发方式中,建议采用@Around方式。

@Component("logger")
@Aspect
public class Logger {
    
    /**
     * 前置通知
     */
    @Before("execution(* com.itheima.service.impl.*.*(..))")
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /**
     * 后置通知
     */
    @AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    
    /**
     * 异常通知
     */
    @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /**
     * 最终通知
     */
    @After("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
    
}

@Pointcut:指定切入点表达式

属性:

​ value:指定表达式的内容(execution关键字)

切入点表达式注需要建立一个函数,private void 任取函数名,函数体为空。任取的函数名为各类通知引用时使用的id。在各类通知的value属性中引用切入点表达式时,注意value里的切入点表达式id名后必须加括号(),否则将会编译错误。

@Component("logger")
@Aspect
public class Logger {
    
	@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt1(){}

    /**
     * 前置通知
     */
    @Before("pt1()")
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /**
     * 后置通知
     */
    @AfterReturning("pt1()")
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    
    /**
     * 异常通知
     */
    @AfterThrowing("pt1()")
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /**
     * 最终通知
     */
    @After("pt1()")
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
    
}

@Around:表明当前方法是一个环绕通知

属性:

​ value:用于指定切入点表达式,还可以指定切入点表达式的引用。

@Component("logger")
@Aspect
public class Logger {
    
	@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt1(){}

    @Around("pt1()")
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");

            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");

            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
    
}

3. 进阶注解(彻底不使用 XML)

@EnableAspectJAutoProxy :开启spring对AOP注解的支持

在本part第1节中的扫描包和AOP注解开启都是用XML配置的

<context:component-scan base-package="com.itheima"/>
<aop:aspectj-autoproxy/>

我们可以在自定义的SpringConfiguration类中用注解配置,彻底和XML说拜拜

@Configuration
@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy
public class SpringConfiguration {
    	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Parker7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值