当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈SpringAOP

浅谈SpringAOP

2019年01月25日  | 移动技术网IT编程  | 我要评论

0. 写在最前面

之前实习天天在写业务,其中有一个业务是非常的复杂,涉及到了特别多的表。最后测下来,一个接口的时间,竟然要5s多。

当时想写一个aop,来计算处理接口花费多长时间,也就是在业务逻辑的前面计算开始的时间,业务逻辑后面计算结束的时间,一相减即可。

但我发觉我竟然忘记怎么写了,哎,没办法,在此纪录,重新捡回吧。

 

1. 什么aop

aop(aspect-oriented programming, 面向切面编程): 是一种新的方法论, 是对传统 oop(object-oriented programming, 面向对象编程) 的补充.

aop 的主要编程对象是切面(aspect), 而切面模块化横切关注点.

在应用 aop 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.

aop 的好处: 每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级 业务模块更简洁, 只包含核心业务代码.

简单来说,aop就是在不影响现有的业务逻辑下面,在业务前面后面织入一些其他逻辑。比如,日志,验证等等

 

2. springaop有哪几种

  • @before: 前置通知, 在方法执行之前执行

  • @after: 后置通知, 在方法执行之后执行

  • @afterrunning: 返回通知, 在方法返回结果之后执行

  • @afterthrowing: 异常通知, 在方法抛出异常之后

  • @around: 环绕通知, 围绕着方法执行

 

假设我们现在有一个接口如下:

1 @requestmapping("/aa")
2 public string a() {
3     person person = new person("zhangsan", 18);
4     redisservice.put("person", person);
5     person ps = redisservice.get("person");
6     return ps.tostring();
7 }

 

现在产品提了一个需求,我想在这段业务执行前,打印一句话,结束后在打印一句话。

很简单,我们直接改业务代码就行了,一个可以两个可以,但是多了呢,很严重影响业务逻辑,此时就可以用到了前置通知和后置通知。

 1 @pointcut(value = "execution (public * com.test.mybatis.demo.web.departmentcontroller.*(..))")
 2 public void pointcut() { }
 3 
 4 @before(value = "pointcut()")
 5 public void beforemethod(joinpoint joinpoint) {
 6     string methodname = joinpoint.getsignature().getname();
 7     list<object> args = lists.newarraylist(joinpoint.getargs());
 8     log.info("the method [" + methodname + "] begins with " + args);
 9 }
10 
11 @after(value = "pointcut()")
12 public void aftermethod(joinpoint joinpoint) {
13     string methodname = joinpoint.getsignature().getname();
14     log.info("the method [" + methodname + "] ends");
15 }

 

解释下这段代码。

pointcut 简称切点,也就是一个表达式,我要在哪些接口上进行拦截,进行一个配置。他有他自己的一套规范,我们按照他的规范来就好了。

joinpoint 简称连接点。程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。joinpoint 我可以获取到方法名,参数等等。

@before,@after 一个是前置通知,一个是后置通知

 此时我再访问这个接口的时候,他的执行流程如下:

 beforemethod  ==>  业务逻辑  ==>  aftermethod

是不是非常的神奇,aop的实现方式可以看我这边博文:

 

 3. 回到我们的最初

如何计算一个接口耗时多少呢?

起初我想到可以用前置 + 后置 来做,但最后相减,变量怎么传递不好搞。想过用全局变量,但并发会出现问题。

此时我想到了很少用的环绕通知,最后代码如下:

 1 @around("pointcut()")
 2 public object aroundmethod(proceedingjoinpoint joinpoint) {
 3     object res = null;
 4     string methodname = joinpoint.getsignature().getname();
 5     try {
 6         // 前置通知
 7         stopwatch stopwatch = stopwatch.createstarted();
 8         // 执行目标方法
 9         res = joinpoint.proceed();
10         // 后置通知
11         long duration = stopwatch.elapsed(timeunit.milliseconds);
12         log.info("{} 执行时长: {}", methodname, duration);
13     } catch (throwable throwable) {
14         throwable.printstacktrace();
15     }
16     return res;
17 }

每次执行一个方法,他都会将你的执行时间打印出来。结果如下:

1 // 前置通知
2 2019-01-24 21:17:52.802  info 224 --- [nio-8080-exec-5] com.test.mybatis.demo.aop.testaop        : the method [testaop] begins
3 // 业务逻辑
4 这里是业务逻辑
5 // 环绕通知
6 2019-01-24 21:17:53.303  info 224 --- [nio-8080-exec-5] com.test.mybatis.demo.aop.testaop        : testaop 执行时长: 500
7 // 后置通知
8 2019-01-24 21:17:53.303  info 224 --- [nio-8080-exec-5] com.test.mybatis.demo.aop.testaop        : the method [testaop] ends

 

4. 写在最后

最后我想将这个计算接口时间的aop定义成一个注解,只要我在接口上加这个注解,他就会打印时间,否则就不打印。

这是后期的一个目标。共勉

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网