Spring Core Programming(Spring核心编程) - AOP Concepts(AOP基本概念)

 

 

 

1. What is aspect-oriented programming?
(什么是面向切面编程?)正则表达式

Aspects help to modularize cross-cutting concerns.
(切面帮助咱们模块化横切关注点)
In short, a cross-cutting concern can be described as any functionality that affects multiple points of an application.
(简单来讲, 一个横切关注点能够理解为任何影响某个应用程序的功能,这个功能会从一些切入点进入程序)
For example, security is a cross-cutting concern, we cam apply security rules to many methods in an application by AOP.
(举例来讲, 安全就是一个横切关注点, 咱们能够用过AOP的方式给应用中的许多方法增长安全逻辑)express

The below picture can help us understand AOP more intuitively编程

(下图能够帮助咱们更加直观地理解AOP编程)安全

2. The benefits of AOPapp

(AOP的好处)less

With AOP, you can define the common functionality in one place,
(经过AOP, 你能够在一个地方定义通用功能)
but you can define how and where this functionality is applied without having to modify the class
to which you’re applying the new feature, thus the class can focus on its main logic.
(你能够经过定义如何而且在何处应用该功能,而不用修改要被影响的那个类,这样被应用的类能够关注它的主逻辑)模块化

 

3. Some core concepts of AOPui

   (AOP的一些核心概念)this

Below are the important terminologies of aspects:
(如下是一些切面的重要术语)spa

advice(通知) / pointcuts(切点) / join points(链接点)

直观如以下:

 

3-1) advice(通知)
The job of an aspect is called advice,
(通知其实就是切面的工做)
it defines what and when of an aspect.
(它定义了这个切面干什么的,以及什么时候使用)

Spring aspects can work with five kinds of advice:
(Spring中能够有5种类型的通知)

* Before(前置通知)
— The advice functionality takes place before the advised method is invoked.
(在目标方法被调用以前调用)
* After(后置通知)
— The advice functionality takes place after the advised method completes, regardless of the outcome.
(在目标方法完成以后调用, 不关心方法的输出)
* After-returning(返回通知)
— The advice functionality takes place after the advised method successfully completes.
(在目标方法成功执行以后调用)
* After-throwing(异常通知)
— The advice functionality takes place after the advised method throws an exception.
(在目标方法抛出异常后调用)
* Around(环绕通知)
— The advice wraps the advised method, providing some functionality before and after the advised method is invoked.
(包裹目标方法, 在目标方法调用以前和调用以后都要调用)

 

3-2) JOIN POINTS(切点)
A join point is a point in the execution of the application where an aspect can be plugged in.
(链接点是在应用执行过程当中插入的点)
could be a method being called, an exception being thrown, or even a field being modified
(能够使调用方法时, 异常抛出时, 或者甚至是一个字段被修改时)

3-3) POINTCUTS(切点)
If advice defines the what and when of aspects, then pointcuts define the where.
(若是说通知定义了切面的什么和什么时候的话,那么切点定义了切面的何处)
Specify pointcuts using explicit class and method names
or through regular expressions that define matching class and method name patterns.
(经过制定类和方法名,或者正则表达式指定的类和方法名称来指定切点)

 

4. SPRING ADVISES OBJECTS AT RUNTIME
(Spring在运行时通知对象)

In Spring, aspects are woven into Spring-managed beans at runtime by wrapping them with a proxy class.
(经过在代理类中包裹切面, Spring在运行时把切面织人到Spring管理的类中)

以下图:

相关文章
相关标签/搜索