面向切片编程(AOP)应用的一些实际例子

简介: 面向切片编程(AOP)应用的一些实际例子

The definition of AOP in wikipedia seems a little bit difficult for beginners to understand, so in this blog I use an example to introduce why we need it.


Suppose I have an order command class which performs its core business logic in method doBusiness:image.pngIn method execute(), it is flooded with too many non-functional code like logging, authorization check and performance trace.

image.pngIt is not a good design, we can try to improve it via template method pattern.


Template method pattern

With this pattern, I create a new parent class BaseCommand, and put all non-functional code inside the executow the real business logic is defined in child class OrderCommand, whose implementation is very clean:image.pngDrawback of this solution: as the parent class has defined the template method execution, it is NOT possible for a child class to adapt it, for example, a child class cannot change the order sequence of authorization check and performance trace method. And suppose a child class does not want to implement authorization check at all – this could not be achieved with this solution. We have to use decorator pattern instead.


Decorator pattern

First I need to create an interface:image.pngAnd create a decorator to cover the log and authorization check function:image.pngAnd a second decorator for performance trace:image.pngAnd the class to finish the real business logic. Now I have the full flexibility to constructor the instance according to real business case, with the help of different decorator. The following instance fullCmd owns the ability of both authorization check log and performance trace.image.pngSuppose in a given scenario, only performance trace is needed, we can just use the performance trace decorator:image.pngDrawback of decorator pattern: The decorator class and the business class have to implement the same interface, command, which is more business related. Is there possibility that the utility classes for non-functional implementation can just work without implementing the same interface which is implemented by business class?


AOP solution

I use a Java project implemented by Spring framework to demonstrate the idea.


Suppose I hope to add performance trace on this business method: saveimage.png1) You may have already observed the annotation @Log(nameI042416=”annotation for save method”) used in line10.

This annotation is declared in file Log.java:image.png(2) Now I have to declare an Aspect class which contains a pointcut. A pointcut tells Spring framework which methods could be applied with AOP strategy, and the class annotated with @Aspect contains methods which should be called by Spring framework to “decorate” the methods identified by annotation. Here below I have declared a pointcut “logJerry” via annotation @Pointcut:image.pngFor example, since we have annotated the business method save() with “@Log(nameI042416=”annotation for save method”)”,

we can define what logics must be done on it, with the help of @Before and @After plus declared pointcut.image.pngWith this approach, I can add performance trace function to save method without modifying its source code.


Set breakpoint on these beforeExec and afterExec methods, launch the project with Tomcat under debug mode, paste the following url to your browser:

http://localhost:9498/springaop/aopRootJerry/aop2Jerry/i042416?string=sap

Through callstack you can understand how the AOP call is working in Spring.image.pngimage.pngWhy we say AOP can increase modularity by allowing the separation of cross-cutting concerns?


Suppose we have lots of methods all of which need common utilities like log, performance trace and authorization check. Before we use AOP, these utilities are scattered in every method:image.pngAfter AOP is used, those common stuff are extracted into Aspect class and reusability is fulfilled. From the picture below we can see the cross-cutting concerns are now separated.image.png








相关文章
|
2月前
|
存储 Java 程序员
Java数组全套深入探究——基础知识阶段2、数组的定义语法
Java数组全套深入探究——基础知识阶段2、数组的定义语法
29 0
|
8月前
|
Java 应用服务中间件 Spring
Spring框架(《面向切面》——对象值的注入——输出) 1
Spring框架(《面向切面》——对象值的注入——输出)
Spring框架(《面向切面》——对象值的注入——输出) 1
|
3月前
对数组中的对象进行排序 简洁易懂
对数组中的对象进行排序 简洁易懂
|
4月前
|
Python
Python函数式编程,举例说明高阶函数的使用。
Python函数式编程,举例说明高阶函数的使用。
|
7月前
|
设计模式 NoSQL Java
如何用最简单的方式解释依赖注入?
如何用最简单的方式解释依赖注入?
44 0
|
7月前
|
NoSQL Redis
如何用最简单的方式解释依赖注入?依赖注入是如何实现解耦的?
如何用最简单的方式解释依赖注入?依赖注入是如何实现解耦的?
28 0
|
8月前
|
Java 数据库 Spring
Spring框架(《面向切面》——对象值的注入——输出) 2
Spring框架(《面向切面》——对象值的注入——输出)
|
10月前
|
设计模式 算法 安全
C++常用的11种设计模式解释及示例
C++常用的11种设计模式解释及示例
|
前端开发
前端学习案例3-Aop切面编程3 原
前端学习案例3-Aop切面编程3 原
41 0
前端学习案例3-Aop切面编程3 原
|
Java
Java接口概念和语法例子(功能性方法)
比如有三个类。兔子、狗、青蛙这三个类。要定义一个公共游泳方法出来。但是兔子不会这个游泳,那么就不使用这个接口,另外的狗和青蛙会游泳,就会使用这个游泳接口。简单来说,就是谁需要功能接口谁就使用这个功能接口就好了
89 0