切面:Aspect
切面=切入点+通知。在老的spring版本中通常用xml配置,现在通常是一个类带上@Aspect注解。切面负责将 横切逻辑(通知) 编织 到指定的连接点中。
目标对象:Target
将要被增强的对象。
连接点:JoinPoint
可以被拦截到的程序执行点,在spring中就是类中的方法。
切入点:PointCut
需要执行拦截的方法,也就是具体实施了横切逻辑的方法。切入点的规则在spring中通过AspectJ pointcut expression language来描述。
切入点与连接点的区别:连接点是所有可以被"切"的点;切入点是真正要切的点。
通知:Advice
针对切入点的横切逻辑,包含“around”、“before”和“after”等不同类型的通知。
通知的作用点如其命名:
before:在切入点之前执行
after:在切入点之后执行
around:在切入点拦截方法,自定义前后,更灵活
还有一些异常处理的通知,这里不一一举例
织入:Weaving
将切面和目标对象连接起来,创建代理对象的过程。spring中用的是动态代理。假如目标对象有接口,使用jdk动态代理;否则使用cglib动态代理。
说了这么多概念,看看代码实现可能会使读者理解的更深刻一些,这里简单写一个通过注解增强方法的AOP-Demo。
首先是切面类:
package com.example.demo.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * @author Fcb * @date 2020/6/20 * @description 切面类=切入点+通知 */ @Aspect @Component public class LogAspect { //这个方法定义了切入点 @Pointcut("@annotation(com.example.demo.aop.anno.MyLog)") public void pointCut() {} //这个方法定义了具体的通知 @After("pointCut()") public void recordRequestParam(JoinPoint joinPoint) { for (Object s : joinPoint.getArgs()) { //打印所有参数,实际中就是记录日志了 System.out.println("after advice : " + s); } } //这个方法定义了具体的通知 @Before("pointCut()") public void startRecord(JoinPoint joinPoint) { for (Object s : joinPoint.getArgs()) { //打印所有参数 System.out.println("before advice : " + s); } } //这个方法定义了具体的通知 @Around("pointCut()") public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable { for (Object s : pjp.getArgs()) { //打印所有参数 System.out.println("around advice : " + s); } return pjp.proceed(); } }
注解:
package com.example.demo.aop.anno; import java.lang.annotation.*; /** * @author Fcb * @date 2020/6/20 * @description */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface MyLog { }
目标类:
package com.example.demo.aop.target; import com.example.demo.aop.anno.MyLog; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @author Fcb * @date 2020/6/20 * @description */ @RestController public class MockController { @RequestMapping("/hello") @MyLog public String helloAop(@RequestParam String key) { System.out.println("do something..."); return "hello world"; } }
最后是测试类:
package com.example.demo.aop.target; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * @author Fcb * @date 2020/6/20 * @description */ @SpringBootTest class MockControllerTest { @Autowired MockController mockController; @Test void helloAop() { mockController.helloAop("aop"); } }
控制台结果:
around advice : aop
before advice : aop
do something...
after advice : aop
推荐教程:《Java教程》
以上是Spring AOP 注解详解的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器