Spring AOP Capabilities and Goals
Spring AOP 使用纯 Java 实现。不需要特殊编译过程。Spring AOP 不需要控制类加载器层次结构,因此适合在 Servlet 容器或应用程序服务器中使用。
Spring AOP is implemented in pure Java. There is no need for a special compilation process. Spring AOP does not need to control the class loader hierarchy and is thus suitable for use in a servlet container or application server.
Spring AOP 目前仅支持方法执行连接点(建议在 Spring bean 上执行方法)。虽然可以在不破坏核心 Spring AOP API 的情况下添加对字段拦截的支持,但并未实现字段拦截。如果你需要建议字段访问并更新连接点,请考虑使用 AspectJ 等语言。
Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.
Spring AOP 对 AOP 的方法与大多数其他 AOP 框架的方法不同。目标不是提供最完整的 AOP 实现(尽管 Spring AOP 非常强大)。相反,目标是提供 AOP 实现与 Spring IoC 之间的紧密集成,以帮助解决企业应用程序的常见问题。
Spring AOP’s approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable). Rather, the aim is to provide a close integration between AOP implementation and Spring IoC, to help solve common problems in enterprise applications.
因此,例如,Spring Framework 的 AOP 功能通常与 Spring IoC 容器结合使用。方面使用常规 bean 定义语法进行配置(尽管这允许强大的“自动代理”功能)。这是与其他 AOP 实现的关键区别。你无法使用 Spring AOP 轻松或有效地做一些事情,例如建议非常细粒度的对象(通常是域对象)。AspectJ 是此类情况的最佳选择。然而,我们的经验是 Spring AOP 为企业 Java 应用程序中大多数适合 AOP 的问题提供了出色的解决方案。
Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured by using normal bean definition syntax (although this allows powerful "auto-proxying" capabilities). This is a crucial difference from other AOP implementations. You cannot do some things easily or efficiently with Spring AOP, such as advise very fine-grained objects (typically, domain objects). AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP.
Spring AOP 从不试图与 AspectJ 竞争以提供全面的 AOP 解决方案。我们相信诸如 Spring AOP 等基于代理的框架和类似 AspectJ 的全面框架都是有价值的,并且它们不是竞争关系,而是互补关系。Spring 将 Spring AOP 和 IoC 与 AspectJ 无缝集成,以便在基于 Spring 的一致应用程序体系结构中启用 AOP 的所有使用方式。此集成不会影响 Spring AOP API 或 AOP Alliance API。Spring AOP 仍然向后兼容。请参阅 the following chapter 以了解 Spring AOP API。
Spring AOP never strives to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks such as Spring AOP and full-blown frameworks such as AspectJ are valuable and that they are complementary, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API. Spring AOP remains backward-compatible. See the following chapter for a discussion of the Spring AOP APIs.
Spring Framework 的中心原则之一是非侵入性。这是一个理念,你不应该被迫将特定于框架的类和接口引入你的业务或域模型。但是,在某些地方,Spring Framework 确实让你可以选择将特定于 Spring Framework 的依赖项引入你的代码库。向你提供此类选项的理由是因为在某些情况下,以这样的方式读取或编码某些特定功能可能更容易。但是,Spring Framework(几乎)总是让你做出选择:你可以自由地做出明智的决定,选择哪种选项最适合你的特定用例或场景。 One of the central tenets of the Spring Framework is that of non-invasiveness. This is the idea that you should not be forced to introduce framework-specific classes and interfaces into your business or domain model. However, in some places, the Spring Framework does give you the option to introduce Spring Framework-specific dependencies into your codebase. The rationale in giving you such options is because, in certain scenarios, it might be just plain easier to read or code some specific piece of functionality in such a way. However, the Spring Framework (almost) always offers you the choice: You have the freedom to make an informed decision as to which option best suits your particular use case or scenario. 与本章相关的一个选择是选择哪种 AOP 框架(和哪种 AOP 风格)。你可以选择 AspectJ、Spring AOP 或同时使用。你还可以选择 @AspectJ 注释风格方法或 Spring XML 配置风格方法。本章选择首先介绍 @AspectJ 风格方法,不应视为 Spring 团队倾向于 @AspectJ 注释风格方法而不是 Spring XML 配置风格方法的迹象。 One such choice that is relevant to this chapter is that of which AOP framework (and which AOP style) to choose. You have the choice of AspectJ, Spring AOP, or both. You also have the choice of either the @AspectJ annotation-style approach or the Spring XML configuration-style approach. The fact that this chapter chooses to introduce the @AspectJ-style approach first should not be taken as an indication that the Spring team favors the @AspectJ annotation-style approach over the Spring XML configuration-style. 请参阅 Choosing which AOP Declaration Style to Use 以更全面地了解每种样式的优点和缺点。 See Choosing which AOP Declaration Style to Use for a more complete discussion of the advantages and disadvantages of each style. |