Introduction to Spring AOP

Source: Internet
Author: User

1. Introduction

AOP (aspect-oriented programming) is a complement to OOP (object-oriented programming), which provides another way of thinking about the structure of a program. The module unit of OOP is class, and the module unit of AOP is aspect. One of the key components in spring is the AOP framework, however, the spring IOC container does not rely on AOP, which means that if you don't want to use AOP, you can.

What is AOP used to do in the spring framework?

    • Provides declarative enterprise services, particularly declarative services instead of EJBS. The most important service is declarative transaction management.
    • Allow user to implement custom aspect
1.1. AOP Concept
    • Aspect (aspect): The modularity of a single point of concern that crosscutting multiple classes. Transaction management is a good example. In spring AOP, aspect can be implemented with ordinary classes or ordinary classes with @aspect annotations.
    • Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In spring AOP, a connection point is always executed on behalf of a method.
    • Advice (Notification): an action taken by a aspect at a particular connection point. The types of notifications include "around", "before", "after". Many AOP frameworks, including spring, also use a notification as an interceptor that maintains an interceptor chain around the connection point.
    • Pointcut (pointcut): a predicate that matches a connection point. Advice associates a pointcut expression. Spring defaults to the aspectj tangent expression.
    • Target object: The object that is notified by one or more aspect. is implemented in spring AOP with a runtime proxy, so this object is always a proxy object.
    • AOP Proxy (AOP agent): An object created by the AOP framework to implement aspect. In the spring framework, an AOP proxy might be a JDK dynamic agent or a cglib agent.
1.2. Type of notification
    • Before advice (pre-notification): notification executed before a connection point, but no ability to block subsequent execution (unless it throws an exception)
    • After returning advice (return notification): Notifications that are executed after a connection point is performed normally: for example, when a method returns without throwing an exception.
    • After throwing advice (exception notification): Notification that the method will execute if it exits because it throws an exception
    • After (finally) advice (post notification): Executes regardless of whether the connection point exits gracefully or exits unexpectedly
    • Around advice (surround notification): a notification that wraps around a connection point such as a method call. This is the strongest kind of notification. Surround notifications can perform custom behavior before or after a method call. It is also responsible for choosing whether to handle the connection point method execution by returning a return of its own or throwing an exception. Surround notifications are the most common kind of notifications.
1.3. Spring AOP capabilities and goals

Spring AOP is implemented in pure Java. Currently only the connection points that the method executes are supported, field interception is not implemented.

1.4. 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 program to interfaces rather than classes, business classes normally would implement one or more business interfaces.

Default JDK dynamic proxy, if the object does not implement the interface then use Cglib

2. @AspectJ Support

@AspectJ is a declarative aspect style that uses annotations to annotate a regular Java class.

2.1. Enable @aspectj Support

You need to turn on spring support for Spring AOP configuration based on @aspectj aspects, and autoproxying beans support. Autoproxying means that if spring detects that a bean is notified by one or more aspect, it will automatically produce a proxy for the bean to intercept the method call and ensure that the notification is executed.

Support for @aspectj can be turned on using XML or Java configuration. You need to make sure that you have aspectweaver.jar under the classpath of your project.

2.2. Declare a aspect

2.3. Declare a Pointcut

A pointcut declaration has both parts:a signature comprising a name and any parameters, and a pointcut expression that Det Ermines exactly which method executions we is interested in. In the @AspectJ Annotation-style of AOP, a pointcut signature are provided by a regular method definition, and the Pointcut Expression is indicated using the @Pointcut annotation (the method serving as the Pointcut signature must has a void RET Urn type).

Declaring a pointcut has two parts: a signature and a pointcut expression. The signature consists of a name and any arguments, and the pointcut expression determines which method execution is of interest. In annotated-style AOP, a pointcut signature is a regular method definition, and the pointcut expression is identified with a @pointcut annotation (the return value of a method that is a pointcut signature must be void)

Spring AOP also supports the following pointcut expressions:

Tangent expressions can be combined using

The format of An execution expression is:

2.4. Declaration Advice

3. AOP support based on XML format

the keywords ' and ', ' or ' and ' not ' can is used in ' && ', ' | | ' and '! ' respectively

4, choose which kind of AOP declaration method 4.1, Spring AOPor full AspectJ?

4.2. @AspectJor XML for Spring AOP?

If you choose to use Spring AOP, then you can choose to use @aspectj or XML style. Recommended for @aspectj.

5. Agent mechanism

Spring AOP uses the JDK dynamic agent or Cglib to create a proxy for the target object. (Preferred JDK dynamic agent)

If the target object of the agent implements at least one interface, then the JDK dynamic Agent will be used. Also, all interfaces implemented by the target object will be proxied.

If the target object does not implement any interfaces, then a CGLIB proxy will be created.

If you want to force the use of the Cglib proxy is also possible, but you need to consider a problem, that is, the final method cannot be notified, because they cannot be overwritten.

If you want to force the use of the Cglib proxy, you can do this by selecting one of the following:

5.1. Understanding AOP Agents

Spring AOP is proxy-based. (Spring AOP is agent-based)

It is extremely important to understand this semantics.

Reference

"Spring Framework Reference Documentation" 4.3.14.RELEASE

Spring documentation

http://repo.springsource.org/libs-release-local/org/springframework/spring/

Introduction to Spring AOP

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.