📅  最后修改于: 2020-11-11 07:06:03             🧑  作者: Mango
Spring框架的关键组件之一是面向方面的编程(AOP)框架。面向方面的编程需要将程序逻辑分解为称为“关注点”的不同部分。跨应用程序多个点的功能称为跨领域关注点,这些跨领域关注点在概念上与应用程序的业务逻辑是分开的。在日志记录,审核,声明性事务,安全性,缓存等方面,存在各种常见的良好示例。
OOP中模块化的关键单元是类,而在AOP中模块化是方面。依赖注入可帮助您将应用程序对象彼此分离,而AOP可帮助您将跨领域关注点与其影响的对象分离。 AOP就像Perl,.NET,Java等编程语言中的触发器一样。
Spring AOP模块提供了拦截器来拦截应用程序。例如,执行方法时,可以在执行方法之前或之后添加其他功能。
在开始使用AOP之前,让我们熟悉AOP的概念和术语。这些术语不是特定于Spring,而是与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. |
Spring方面可以使用以下五种建议-
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. |
Spring支持@AspectJ批注样式方法和基于模式的方法来实现自定义方面。以下各节详细说明了这两种方法。
Sr.No | Approach & Description |
---|---|
1 | XML Schema based
Aspects 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. |