Spring AOP framework

Spring框架中的面向切面编程(AOP):概念、实现与定制

A key component of the Spring framework is the Aspect Oriented Programming (AOP) framework. Aspect oriented programming needs to decompose program logic into different parts called so-called concerns. Functions that span multiple points of an application are called crosscutting concerns, which are conceptually independent of the application’s business logic. There are various common examples of good aspects, such as logging, auditing, declarative transactions, security, and caching.
In OOP, key unit modularity is a class, while in AOP, unit modularity is an aspect. Dependency injection helps you decouple application objects from each other and AOP helps you decouple crosscutting concerns from the objects they affect. AOP is like the trigger of programming language, such as Perl NET, Java or others.
The Spring AOP module provides interceptors to intercept an application. For example, when executing a method, you can add additional functions before or after the method execution.

AOP terminology

Before we begin to work with AOP, let’s familiarize ourselves with AOP concepts and terminology. These terms are not specific to Spring, but are related to AOP.

termdescribe
AspectA module has a set of APIs that provide crosscutting requirements. For example, a log module will be called by the AOP aspect to record logs. An application can have any number of aspects, depending on the requirements.
Join pointIt represents a point in your application, and you can do it in terms of plug-in AOP. You can also say that it is in the actual application, and one of the operations will use the Spring AOP framework.
AdviceThis is the method of implementation before or after the actual action. This is the code that is actually called through the Spring AOP framework during program execution.
PointcutThis is a group of one or more connection points, and the notification should be executed. You can use expressions or patterns to specify pointcuts, as we will see in the AOP example.
IntroductionReferences allow you to add new methods or properties to existing classes.
Target objectThe object notified by one or more aspects is always a proxy object. Also known as the notified object.
WeavingWeaving connects aspects to other application types or objects and creates a notified object. This can be done at compile time, class load time, and run time.

Type of notification

Spring can use the following five notifications:

Advicedescribe
Pre notificationBefore a method is executed, the notification is executed.
Post notificationAfter a method is executed, regardless of its outcome, execute the notification.
Notification after returnAfter a method is executed, notifications can only be executed when the method is successfully completed.
Notify after throwing an exceptionAfter a method is executed, notifications can only be executed when the method exits and throws an exception.
Surround notificationExecute notifications before and after suggesting method calls.

Implement custom aspects

Spring supports @ AspectJ annotation style and pattern based methods to implement custom aspects. These two methods have been explained in detail in the following two subsections.

methoddescribe
XML Schema basedThe aspect is implemented using regular classes and configuration based XML.
@AspectJ based@AspectJ references a declarative style as a regular Java class annotation with Java 5 annotations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值