设为首页 加入收藏

TOP

Spring AOP与AspectJ的对比及应用(二)
2023-07-26 08:17:13 】 浏览:85
Tags:Spring AOP AspectJ 应用
hod); log.info("------pkslow aop doBefore end------"); } @Around("testControllerPointcut()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { log.info("------pkslow aop doAround start------"); long start = System.nanoTime(); Object obj = joinPoint.proceed(); long end = System.nanoTime(); log.info("Execution Time: " + (end - start) + " ns"); log.info("------pkslow aop doAround end------"); return obj; } }

@Pointcut定义哪些类和方法会被捕抓来代理,这里配置的是controller下的所有方法。

@Before@Around则定义了一些处理逻辑。@Before是打印了方法名,而@Around是做了一个计时。

注意:是不需要配置@EnableAspectJAutoProxy的。

3.4 maven插件

因为是需要编译时织入代码,所以需要maven插件的支持:https://github.com/mojohaus/aspectj-maven-plugin

配置好pom.xml文件即可。

然后执行命令打包:

mvn clean package

这时会显示一些织入信息,大致如下:

[INFO] Join point 'method-execution(java.lang.String com.pkslow.springboot.controller.TestController.hello())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:14) advised by around advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))
[INFO] Join point 'method-execution(java.lang.String com.pkslow.springboot.controller.TestController.hello())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:14) advised by before advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))
[INFO] Join point 'method-execution(void com.pkslow.springboot.controller.TestController.test())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:22) advised by around advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))
[INFO] Join point 'method-execution(void com.pkslow.springboot.controller.TestController.test())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:22) advised by before advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))
[INFO] Join point 'method-execution(void com.pkslow.springboot.controller.TestController.staticTest())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:28) advised by around advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))
[INFO] Join point 'method-execution(void com.pkslow.springboot.controller.TestController.staticTest())' in Type 'com.pkslow.springboot.controller.TestController' (TestController.java:28) advised by before advice from 'com.pkslow.springboot.aop.ControllerAspect' (ControllerAspect.class(from ControllerAspect.java))

看到以上信息,说明成功织入了代码,具体可以查看生成的class文件。

可以看到有许多代码都不是我们写的,而是织入生成。

3.5 执行及测试

编译成功后,我们就执行代码。如果是通过IDEA来执行,则在运行前不需要再build了,因为已经通过maven build过了包。通过IDEA自带的编译器build,可能无法织入。或者选择ajc作为编译器。具体请参考:IDEA启动Springboot但AOP失效

访问如下:

GET http://localhost:8080/test/hello

则日志如下,成功实现AOP功能:

3.6 一些遇到的错误

遇到错误:

ajc Syntax error, annotations are only available if source level is 1.5 or greater

需要配置插件:

<complianceLevel>${java.version}</complianceLevel>
<source>${java.version}&
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HashMap源码分析 (基于JDK1.8) 下一篇spi~在插件开发过程中的使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目