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管理的類中)

以下圖:

相關文章
相關標籤/搜索