Spring 简明教程
AOP with Spring Framework
Spring Framework 的关键组件之一是 Aspect oriented programming (AOP) 框架。面向方面编程将程序逻辑细分为称为关注点的不同部分。跨越应用程序多个点的函数称为 cross-cutting concerns ,这些跨领域关注点在概念上与应用程序的业务逻辑分离。记录、审核、声明式事务、安全、缓存等方面有很多常见的例子。
One of the key components of Spring Framework is the Aspect oriented programming (AOP) framework. Aspect-Oriented Programming entails breaking down program logic into distinct parts called so-called concerns. The functions that span multiple points of an application are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the application’s business logic. There are various common good examples of aspects like logging, auditing, declarative transactions, security, caching, etc.
面向对象编程中模块化的关键单元是类,而在面向方面编程中,模块化的单元是方面。依赖注入可帮助你解除应用程序对象之间的耦合,面向方面编程可帮助你解除跨领域关注点与它们所影响的对象之间的耦合。面向方面编程类似于 Perl、.NET、Java 和其他编程语言中的触发器。
The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Dependency Injection helps you decouple your application objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect. AOP is like triggers in programming languages such as Perl, .NET, Java, and others.
Spring AOP 模块提供拦截器来拦截应用程序。例如,当执行方法时,你可以在方法执行之前或之后添加额外功能。
Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.
AOP Terminologies
在我们开始使用面向方面编程之前,让我们熟悉面向方面编程的概念和术语。这些术语并非特定于 Spring,而是与面向方面编程相关。
Before we start working with AOP, let us become familiar with the AOP concepts and terminology. These terms are not specific to Spring, rather they are related to AOP.
Sr.No |
Terms & Description |
1 |
Aspect This is a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. |
2 |
Join point This represents a point in your application where you can plug-in the AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework. |
3 |
Advice This is the actual action to be taken either before or after the method execution. This is an actual piece of code that is invoked during the program execution by Spring AOP framework. |
4 |
Pointcut This is a set of one or more join points where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples. |
5 |
Introduction An introduction allows you to add new methods or attributes to the existing classes. |
6 |
Target object The object being advised by one or more aspects. This object will always be a proxied object, also referred to as the advised object. |
7 |
Weaving Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime. |
Types of Advice
@Spring 方面可使用如下所述的五种建议进行处理:
Spring aspects can work with five kinds of advice mentioned as follows −
Sr.No |
Advice & Description |
1 |
before Run advice before the a method execution. |
2 |
after Run advice after the method execution, regardless of its outcome. |
3 |
after-returning Run advice after the a method execution only if method completes successfully. |
4 |
after-throwing Run advice after the a method execution only if method exits by throwing an exception. |
5 |
around Run advice before and after the advised method is invoked. |
Custom Aspects Implementation
Spring 支持 @AspectJ annotation style 方法和 schema-based 方法来实现自定义方面。这两个方法在以下部分中已作了详细说明。
Spring supports the @AspectJ annotation style approach and the schema-based approach to implement custom aspects. These two approaches have been explained in detail in the following sections.
Sr.No |
Approach & Description |
1 |
XML Schema basedAspects are implemented using the regular classes along with XML based configuration. |
2 |
@AspectJ based@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations. |