Spring AOP 4 Official documents

Source: Internet
Author: User
Tags aop join joins maven central
Aspect oriented programming with Spring 11.1 Introduction

Aspect-oriented Programming (AOP) complements object-oriented programming (OOP) by providing another how to thinking about Program structure. The key unit of modularity in OOP are 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 is often termed crosscutting concerns in AOP literature.)

One of the key components of the Spring is the AOP framework. While the Spring IoC container does isn't depend on AOP, meaning you does not need to use AOP if you don ' t want to, AOP comple ments Spring IoC to provide a very capable middleware solution.

Spring 2.0 AOP

Spring 2.0 introduces a simpler and more powerful the-a-writing custom aspects using either a schema-based approach or th e @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ Pointcut language, while still using Spring AOP for W Eaving.

The Spring 2.0 Schema-and @AspectJ-based AOP support are discussed in this chapter. Spring 2.0 AOP remains fully backwards compatible with Spring 1.2 AOP, and the Lower-level AOP support offered by the SPRI NG 1.2 APIs are discussed in the following chapter.

AOP is used in the Spring Framework to ... provide declarative enterprise services, especially as a replacement for EJB D Eclarative Services. The most important such service is declarative transaction management. ... allow users to implement custom aspects, complementing their use of the OOP with AOP.

If you is interested only in generic declarative services or other pre-packaged declarative middleware services such as P Ooling, you don't need to work directly with Spring AOP, and can skip the most of the this chapter.

11.1.1 AOP Concepts

Let us begin by defining some central AOP concepts and terminology. These terms was not spring-specific ... Unfortunately, AOP terminology was not particularly intuitive; However, it would is even more confusing if Spring used its own terminology. Aspect:a Modularization of a concern that cuts across multiple classes. Transaction Management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects is implemented using regular classes (the schema-based approach) or regular classes annotated With the  @Aspect  annotation (the  @AspectJ  style). Join Point:a Point during the execution of a, such as the execution of a method or the handling of a exception. In Spring AOP, a joins point always represents a method execution. Advice:action taken by an aspect to a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types is discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join P Oint. POINTCUT:A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the EXECU tion of a method with a certain name). The concept of join points as matched by pointcut expressions are central to AOP, and Spring uses the AspectJ pointcut expr Ession language by default. Introduction:declaring additional methods or fields on behalf of a type. Spring AOP allows introduce new interfaces (and a corresponding implementation) to any advised object. For example, the could use of a introduction to make a beans implement an ismodified interface, to simplify caching . (An introduction is known as a inter-type declaration in the AspectJ community.) Target Object:object being advised by one or more aspects. Also referred to as The advised object. Since SpringAOP is implemented using runtime proxies, the This object would always be a proxied object. AOP Proxy:an object created by the AOP framework in order to implement the aspect contracts (advise method executions and So on). The Spring Framework, an AOP proxy would be a JDK dynamic proxy or a CGLIB proxy. Weaving:linking aspects with and application types or objects to create an advised object. This can is done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

Types of Advice:before Advice:advice that executes before a join point, but which does not has the ability to Preven T execution flow proceeding to the join point (unless it throws an exception). After returning Advice:advice to is executed after a joins point completes normally:for example, if a method returns with Out throwing an exception. After throwing Advice:advice to is executed if a method exits by throwing an exception. After (finally) Advice:advice to is executed regardless of the means by which a join point exits (normal or exceptional R Eturn). Around Advice:advice that surrounds a joins point such as a method invocation. The most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by re Turning its own return value or throwing an exception.

Around advice is the most general kind of advice. Since Spring AOP, like AspectJ, provides a full range of advice types, we recommend so use the least powerful advice Type that can implement the required behavior. For example, if you need only to update a cache with the return value of a method, you is better off implementing an afte R returning advice than an around advice, although an around advice can accomplish the same thing. Using the most specific advice type provides a simpler programming model with less potential for errors. For example, if need to invoke the proceed () method on the joinpoint used for around advice, and hence cannot fail to invoke it.

In Spring 2.0, all advice parameters is statically typed, so it's work with advice parameters of the appropriate type (The type of the return value from a method execution for example) rather than Object arrays.

The concept of join points, matched by pointcuts, are the key to AOP which distinguishes it from older technologies Offerin G only interception. Pointcuts enable advice to be targeted independently of the object-oriented hierarchy. For example, an around advice providing declarative transaction management can be applied to a set of methods spanning Mul Tiple objects (such as all business operations in the service layer). 11.1.2 Spring AOP capabilities and goals

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 are thus suitable for use in a Servlet container or app Lication server.

Spring AOP currently supports only method execution joins points (advising the execution of methods on Spring beans). Field interception isn't implemented, although support for field interception could be added without breaking the core Sp Ring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.

Spring AOP's approach to the AOP differs from the most of the other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable); It is rather to provide a close integration between AOP implementation and Spring IoC to help solve common problems in ENT Erprise applications.

Thus, for example, the spring Framework's AOP functionality is normally used in conjunction with the spring IoC contain Er. Aspects is configured using normal bean definition syntax (although this allows powerful "autoproxying" capabilities): th Is was a crucial difference from other AOP implementations. There is some things you cannot does easily or efficiently with Spring AOP, such as advise very fine-grained objects (such As domain objects typically): AspectJ is the best choice in such cases. However, our experience are that Spring AOP provides a excellent solution to the most problems in enterprise Java applications That is amenable to AOP.

Spring AOP would never strive to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks like Spring AOP and full-blown frameworks such as AspectJ is valuable, and th At they is complementary, rather than in competition. Spring seamlessly integrates spring AOP and IoC with AspectJ, to-enable all uses of AOP to is catered for within a consist ENT 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.

One of the central tenets of the Spring Framework was that of non-invasiveness; OT be forced to introduce framework-specific classes and interfaces into your business/domain model. However, in some places the spring Framework does give your the option to introduce Spring framework-specific dependencies into your codebase:the rationale in giving you such options are because in certain scenarios it might be just plain easier To read or code some specific piece of functionality in such a. The Spring Framework (almost) always offers you the choice though:you has the freedom to make a informed decision as to Which option best suits your particular with case or scenario.

One such choice that's relevant to this chapter are that of the which AOP framework (and which AOP style) to choose. You has the choice of AspectJ and/or Spring AOP, and you also has the choice of either the @AspectJ Annotation-style app Roach or the Spring XML configuration-style approach. The fact that this chapter chooses to introduce the @AspectJ-style approach first should not being taken as an indication tha T the Spring team favors the @AspectJ annotation-style approach over the spring XML Configuration-style.

See section 11.4, ' choosing which AOP declaration style to use ' for a more complete discussion of the whys and wherefores of each style.

11.1.3 AOP Proxies

Spring AOP defaults to the using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

Spring AOP can also use CGLIB proxies. This was necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to interfaces rather than classes; Business classes normally would implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that's not de Clared on the interface, or where you need to pass a Proxied object to a method as a concrete type.

It's important to grasp the fact, Spring AOP is proxy-based. See sections 11.6.1, "Understanding AOP proxies" for a thorough examination of exactly what's this implementation detail ACTU Ally means. 11.2 @AspectJ Support

@AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations. The @AspectJ style is introduced by the AspectJ project as part of the AspectJ 5 release. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching . The AOP runtime is still pure Spring AOP though, and there are no dependency on the AspectJ compiler or Weaver.

Using the AspectJ compiler and Weaver enables use of the full AspectJ language, and are discussed in section 11.8, "Using A SPECTJ with Spring applications ".

11.2.1 enabling @AspectJ support

To use @AspectJ aspects in a spring configuration need to enable spring support for configuring spring AOP based on @A SPECTJ aspects, and autoproxying beans based on whether or not they is advised by those aspects. By autoproxying we mean so if Spring determines that a bean was advised by one or more aspects, it would automatically Gen Erate a proxy for the bean to intercept method invocations and ensure that advice are executed as needed.

The @AspectJ support can is enabled with an XML or Java style configuration. In either case you'll also need to ensure that AspectJ ' s Aspectjweaver.jar library are on the classpath of your Applicati On (version 1.6.8 or later). This library was available in the ' Lib ' Directory of a AspectJ distribution or via the Maven central repository. enabling @AspectJ support with Java configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.