The concept of Spring AOP

The concept of Spring AOP

 

Why use AOP?

 

      1. Save code, add facets to complete specific operations in a unified manner, without having to write each class

      2. In order to make the organization clearer, the code focuses on the business, and other things (security, transaction, log) let the aspects complete

 

 

 

The technologies for implementing AOP are mainly divided into two categories:

 

      1. Using dynamic proxy technology, the message is decorated by intercepting the message to replace the execution of the original object behavior;

      2. In the way of static weaving, a specific syntax is introduced to create "aspects", so that the compiler can weave code about "aspects" during compilation.

 

 

 

AOP usage scenarios

 

     Authentication permission

      Caching

      Context passing content passing

      Error handling

      Lazy loading

      Debugging

      logging, tracing, profiling and monitoring

      Performance optimization

      Persistence persistence

      Resource pooling Resource pooling

      Synchronization

      Transactions

 

      1. Performance monitoring: record the call time before and after the method call, and alarm if the method execution is too long or timed out.

      2. Cache proxy: The return value of a method is cached, and the next time the method is executed, it is obtained directly from the cache.

      3. Software cracking: Use AOP to modify the judgment logic of the verification class of the software.

      4. Workflow system: The workflow system needs to mix the business code and the process engine code for execution. AOP can be used to separate them and dynamically hook up the business.

      5. Permission verification: Verify whether you have permission to execute the current method before the method is executed.

      6. After the method is called successfully --> count the number of calls --> store it in the cache server --> store it in the database every day

         Because the number of method calls per day is nearly one million, in order to reduce the pressure on the database, it cannot be stored in real time.

      7. 事务:在监控的方法前后加环绕通知,前通知负责记录SQL,后通知负责提交事务或回滚。

 



 

 

spring的术语

 

Advice(通知)

在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。Spring中定义了四个advice: BeforeAdvice, AfterAdvice, ThrowAdvice和DynamicIntroductionAdvice

 

Aspect(切面)

是个类,类里写了想要横切进去的逻辑。

 

JoinPoint(连接点)

可以在系统中织入横切逻辑的位置

 

PointCut(切入点)

是表达式,用来描述系统中所有需要织入横切逻辑的 Joinpoint 的集合 

 

introduction(引入)

允许我们向现有的类添加新方法属性。这不就是把切面(也就是新方法属性:通知定义的)用到目标类中吗

 

Target Object(目标对象)

被一个或多个切面所通知的对象。Spring AOP通过运行时代理实现,因此,这个对象永远是一个被代理对象。

 

AOP Proxy(AOP代理)

AOP框架创建的对象,用来实现你想要的切入功能。Spring中,AOP代理是JDK动态代理或者CGLIB代理。

 

Weave(织入)

把切面连接到其他的应用程序类型或者对象上,并创建一个被通知对象。织入可以在编译时,类加载时和运行时完成,Spring是在运行时完成的。

 



 

 

开发中与代码有关的就是

   1. 切面

   2. 通知

   3. 切入点

 

 

 

各种通知(Advice)类型

 

Before advice:在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。

ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。

 

After advice:当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。

 

After returnadvice:在某连接点正常完成后执行的通知,不包括抛出异常的情况。

ApplicationContext中在<aop:aspect>里面使用<aop:after-returning>元素进行声明。

 

Around advice:包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。

ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。

 

Afterthrowing advice:在方法抛出异常退出时执行的通知。

 

ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。

 



 

 

 

AOP 2种代理的区别

 

      1. JDK基于接口实现:JDK动态代理对实现了接口的类进行代理。

 

      2. CGLIB基于继承:CGLIB代理可以对类代理,主要对指定的类生成一个子类,因为是继承,所以目标类最好不要使用final声明。 

       

      通常情况下,鼓励使用jdk代理,因为业务一般都会抽象出一个接口,而且不用引入新的东西。如果是遗留的系统,以前没有实现接口,那么只能使用CGLIB。

 

 

 

参考:

http://www.myexception.cn/software-architecture-design/925289.html

http://www.th7.cn/Program/java/2012/03/03/62048.shtml 

http://blog.csdn.net/moreevan/article/details/11977115/

http://blog.csdn.net/dreamthen/article/details/26687727

http://blog.csdn.net/xiaoxian8023/article/details/17225339

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568855&siteId=291194637