Spring's Aspect oriented programming with spring

Source: Internet
Author: User
Tags aop

1. Concepts

aspect-oriented Programming (AOP) complements OOP by providing another the thinking about program structure. while OO decomposes applications to a hierarchy of objects, AOP decomposes programs into Aspects or concerns. This enables modularization of concerns such as transaction management that would otherwise cut across multiple objects. (Such concerns is often termed crosscutting concerns.)

One of the key components of the Spring is the AOP framework. While the Spring IoC containers (beanfactory and ApplicationContext) does not depend on AOP, meaning you don ' t need to use A OP If you don't want to, the AOP complements Spring IoC to provide a very capable middleware solution.

AOP is used in Spring:

    • To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service are declarative transaction Management, which builds on Spring ' s transaction a Bstraction.

    • To allow the users to implement the custom aspects, complementing their use of the OOP with AOP.

Thus You can view spring AOP as either a enabling technology that allows Spring to provide declarative transaction manage ment without EJB; Or use the full power of the Spring AOP framework to implement custom aspects.

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.

1.1. AOP Concepts

Let us begin by defining some central AOP concepts. These terms is not spring-specific. Unfortunately, AOP terminology is not particularly intuitive. However, it would is even more confusing if Spring used its own terminology.

  • Aspect: A modularization of a concern for which the implementation might otherwise cut across multiple object S. Transaction Management is a good example of a crosscutting concern in Java EE applications. Aspects is implemented using Spring as Advisors or interceptors.

  • joinpoint: Point during the execution of a program, such as a method invocation or a particular exception being T Hrown. In Spring AOP, a joinpoint are always method invocation. Spring does not use the term joinpoint prominently; Joinpoint information is accessible through methods in the methodinvocation argument passed to interceptors, and is evaluated by implementations of the Org.springframework.aop.Pointcut interface.

  • Advice: Action taken by the AOP framework at a particular joinpoint. Different types of advice include "around," "before" and "throws" advice. Advice types is discussed below. Many AOP frameworks, including Spring, model an advice as aninterceptor, maintaining a chain of interceptors "Around" the Joinpoint.

  • Pointcut: A set of joinpoints specifying when an advice shouldfire. An AOP framework must allow developers to specify Pointcuts:for example, using regular expressions. (Joinpoint Collection)

  • Introduction: Adding methods or fields to an advised class. Spring allows introduce new interfaces to any advised object. For example, you could use a introduction to make any object implement a ismodified interface, to simplify Cach Ing.

  • Target Object: object containing the Joinpoint. Also referred to as advised or proxied object (proxied or enhanced).

  • AOP proxy: Object created by the AOP framework, including advice. In Spring, an AOP proxy would be a JDK dynamic proxy or a CGLIB proxy.

  • Weaving: assembling aspects to create an advised object. This can is done on compile time (using the AspectJ compiler, for example), or at runtime. Spring, like other pure Java AOP frameworks, performs weaving at runtime.

Spring's Aspect oriented programming with spring

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.