Introduction to spring related-AOP programming

Source: Internet
Author: User

1 AOP Overview

AOP (aspect-oriented programming, aspect-oriented programming ): is a new methodology for traditional OOP ( Object-oriented programming, Object-oriented programming ) complements.

Role: Essentially a way to simplify code
Inheritance mechanism
Encapsulation method
Dynamic Agent
......

The main object of the AOP programming operation is the tangent plane (aspect), while the facets are modular crosscutting concerns .

When you apply AOP programming, you still need to define public functionality, but you can clearly define where the feature is applied, how it is applied, and you do not have to modify the affected classes. Such a crosscutting concern is modularized into a special class -a class that we often call "facets."

Benefits of AOP:

0 Each thing logic in one place, code is not scattered, easy to maintain and upgrade

0 business modules are more concise and contain only core code

2 AOP Terminology

2.1 crosscutting concerns

the same class of non-core business extracted from each method. (process non-core business from extraction to method)

2.2 Facets (Aspect)

Package class for crosscutting concern information , each Focus Point reflected as a notification method.

2.3 Notice (Advice)

The specific work to be done by the facets

2.4 goal (target )

The object being notified

2.5 Agent (proxy )

after the notification is applied to the target object Proxy Object

2.6 Connection point (joinpoint)

The specific embodiment of crosscutting concerns in program code that corresponds to a specific location that the program executes. For example: before a method call, after a call, after a method catches an exception, and so on.

in your application, you can use the horizontal two coordinates to locate a specific connection point:

2.7 entry point (pointcut):

the way to locate the connection point. Each class has multiple connection points in its methods, so the connection point is something that exists in the class. If you think of a connection point as a record in a database, the pointcut is the query condition --AOP can be positioned to a specific connection point through a pointcut.

pointcuts are described by the Org.springframework.aop.Pointcut interface, which uses classes and methods as query criteria for connection points.

3 AspectJ

3.1 Introduction

AspectJ:the most complete and popular AOP Framework in theJava community .

in the Spring2.0 or later, you can use AOP based on AspectJ annotations or XML-based configuration .

3.2 Enable AspectJ annotation support in Spring

① importing JAR Packages

Aopalliance.jar

Aspectj.weaver.jar

Spring-aspects.jar

② introducing an AOP namespace

③ Configuration

<aop:aspectj-autoproxy>

when When the Spring IOC container detects the <aop:aspectj-autoproxy> elements in the bean configuration file , it automatically AspectJ Facets match the Bean Create an agent

3.3 declaring slices with AspectJ annotations

① to declare AspectJ facets in Spring , simply declare the facets as beans in the IOC container Instance.

② when in Spring IOC Initialize in container AspectJ after slicing, Spring IOC The container will be for those with AspectJ The facets match the Bean Create the agent.

③ in the AspectJ Annotation, the slice is just a Java class with @Aspect annotations , which often contains a lot of notifications.

④ notifications are simple Java methods that annotate some kind of annotation .

⑤AspectJ supports 5 Types of notification annotations:

[1] @Before: Pre-notification, execution before method execution

[2] @After: Post notification, executed after method execution

[3] @AfterRunning: Returns a notification, executed after the method returns the result

[4] @AfterThrowing: Exception notification, executed after the method throws an exception

[5] @Around: Surround notification, around method execution

A detailed description of the notice:

1.1 Overview

[1] The action to perform on the specific connection point.

[2] a slice can include one or more notifications.

[3] The value of the annotation used by the notification is often the pointcut expression.

1.2 front-facing notification

forward Notification: A notification that is executed before the method executes

   using @Before annotations

1.3 post-placement notification

Post notification: The post notification is executed after the connection point is completed, that is, when the connection point returns a result or throws an exception

using @After annotations

1.4 Return Notification

Return notification: The post notification executes regardless of whether the connection point returns gracefully or throws an exception. If you want to log only when the connection point is returned, you should use the return notification instead of the post notification.

using @AfterReturning annotations

Note: The return value of the connection point is accessed in the return notification    

    • In the return notification, the return value of the connection point can be accessed as long as the returning attribute is added to the @AfterReturning annotation. The value of this property is the parameter name that is used to pass in the return value
    • You must add a parameter with the same name in the signature of the notification method. At run time, Spring AOP passes the return value through this parameter
    • The original pointcut expression needs to appear in the Pointcut property

1.5 Exception Notification

Exception notification: Exception notification is only performed when the connection point throws an exception

Add the throwing property to the @AfterThrowing Annotation, or you can access the exception thrown by the connection point. throwable is the top-level parent class for all errors and exception classes, so you can catch any errors and exceptions in the exception notification method.

If you are only interested in a particular type of exception, you can declare the parameter as the parameter type of the other exception. The notification is then executed only if the exception of the type and its subclasses is thrown

1.6 Surround Notification

[1] Surround notification is the most powerful of all notification types, provides full control over connection points, and even controls whether or not to perform connection points.

[2] for surround notification, the parameter type of the connection point must be Proceedingjoinpoint. It is a Sub-interface of the joinpoint, allowing control over when to execute and whether to execute a connection point.

[3] need to explicitly call in surround notification Proceedingjoinpoint The proceed () method to execute the Proxied method. Forgetting to do so causes the notification to be executed, but the target method is not executed.

[4] Note: The method surrounding the notification needs to return the result after the execution of the target method, that is, call joinpoint.proceed (); return value, or a null pointer exception will occur.

Introduction to spring related-AOP programming

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.