Aspect Oriented Programming with Spring
Spring AOP框架补充了Spring IoC容器,提供了强大的中间件解决方案。Spring提供基于架构或@AspectJ注解的简单方式编写自定义切面,同时使用AspectJ切入点语言。
本章讨论基于架构和@AspectJ的AOP支持,而较低级别的AOP支持将在后续章节中讨论。Spring AOP在框架中用于提供声明性企业服务,如事务管理,以及用户实现自定义切面,用AOP补充OOP。
面向对象的编程(OOP)和面向切面的编程(AOP)相辅相成,提供了另一种思考程序结构的方式。OOP 中模块化的关键单位是类,而在 AOP 中,模块化的单位是切面。切面能将横跨多种类型和对象的问题(例如事务管理)模块化。(在 AOP 文献中,此类问题通常被称为“横切”问题。)
Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns (such as transaction management) that cut across multiple types and objects. (Such concerns are often termed "crosscutting" concerns in AOP literature.)
Spring 的关键组件之一是 AOP 框架。虽然 Spring IoC 容器不依赖于 AOP(这意味着您不必使用 AOP,如果不想要的话),但 AOP 补充了 Spring IoC,提供了一个非常有能力的中间件解决方案。
One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP (meaning you do not need to use AOP if you don’t want to), AOP complements Spring IoC to provide a very capable middleware solution.
Spring 通过使用基于架构的方法或 @AspectJ 注解样式提供编写自定义切面的简单而强大的方式。这两种样式都提供完全输入的建议,并使用 AspectJ 切入点语言,同时仍然使用 Spring AOP 进行绑定。
Spring provides simple and powerful ways of writing custom aspects by using either a schema-based approach or the @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ pointcut language while still using Spring AOP for weaving.
本章讨论基于 schema 和 @AspectJ 的 AOP 支持。在以下章节中讨论较低级别的 AOP 支持。
This chapter discusses the schema- and @AspectJ-based AOP support. The lower-level AOP support is discussed in the following chapter.
AOP 在 Spring 框架中用于:
AOP is used in the Spring Framework to:
-
Provide declarative enterprise services. The most important such service is declarative transaction management.
-
Let users implement custom aspects, complementing their use of OOP with AOP.
如果您仅对通用声明性服务或其他预打包的声明性中间件服务(如池)感兴趣,则您不必直接使用 Spring AOP,并且可以跳过本章的大部分内容。 |
If you are interested only in generic declarative services or other pre-packaged declarative middleware services such as pooling, you do not need to work directly with Spring AOP, and can skip most of this chapter. |