[刘阳Java]_Spring AOP注解详细介绍_第8讲
这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用
1. 要使用Spring AOP注解,必须满足如下的事项
- 导入Aspectj的jar、Spring3.0-AOP.jar、aopalliance.jar
- 需要在配置文件中加入注解的配置,例如:bean-aop-annotiation.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
如果使用Spring AOP注解,最好的用处就是减少在配置文件中的AOP内容。但是如果要掌握好Spring的AOP还需要学习注解的语法,下面的内容会给大家慢慢介绍
2. 织入点语法
package com.spring.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect public class LogAop { @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))") public void logBefore() { System.out.println("方法执行之前转载日志"); } }
而execution(public void com.spring.dao.impl.StudentDaoImpl.*(..)),这个就是织入点的语法,它告诉AOP框架哪个类中方法需要进行AOP
3. execution语法介绍
- execution(public * *(..))
- execution(* set*(..))
- execution(* com.xyz.service.AccountService.*(..))
- execution(* com.xyz.service..*.*(..))
- 上面只是举例说明了execution的语法,下面是一个标准的语法定义
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
4. Spring AOP注解例子
- @Before前置建议,它是在执行一个业务方法之前插入的切面
- @AfterReturning,它是当一个方法正常运行后,执行的切面
- @After,它是当方法执行成功或者出现异常的时候都会执行切面
- @Around,它相当于一个AOP链,如果当前AOP执行后,就让下一个AOP执行
- @AfterThrowing,如果在方法中有错误抛出,则执行此建议
package com.spring.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect public class LogAop { @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))") public void logBefore() { System.out.println("方法执行之前转载日志"); } @AfterReturning("execution(public void com.spring.dao.impl.StudentDaoImpl.insert(..))") public void logAfterReturning() { System.out.println("方法执行返回后载入日志"); } @After("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))") public void logAfter() { System.out.println("Finally载入日志"); } @Around("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))") public Object doBasicProfiling(ProceedingJoinPointpjp) throws Throwable { System.out.println("===around建议载入日志===" + new Date()); Object o = pjp.proceed(); return o; } @AfterThrowing("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))") public void logAfterThrowing() { System.out.println("===有参数异常载入日志===" + new Date()); } }
分类:
Spring_学习笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下