Spring Series Spring AOP programming for facets

Source: Internet
Author: User
Tags aop

Objective

After an article was used in the morning to do anti-duplication control, this article focuses on the aspect of AOP.

In development, there are functional behaviors that are common, such as log management, security, and transactions, and they have one thing in common that is distributed across multiple applications, a feature called crosscutting concerns (cross-cutting concerns).

DI (Dependency injection) facilitates decoupling between objects, and AOP enables decoupling between crosscutting concerns and the objects they affect.

Aspect-oriented programming in spring AOP there are 4 types of calls, method calls before, after, exceptions to add other methods, method calls before and after the call to other methods, the method of the parameters passed to other methods, a new amount of interface implementation as the parent class interface to other methods. When we call a method, Spring AOP does something extra for this method in other Le types.

First, what is aspect-oriented programming?

Inheritance and delegation are the most common object-oriented techniques for implementing common functionality, and facets provide another alternative to inheritance and delegation, and are clearer and more concise in many scenarios.

While we are still defining common functionality in one place while using face-oriented programming, we can provide a declarative way to define how this function is applied in any way, without modifying the affected classes.

Crosscutting concerns can be modularized into special classes, which are called facets.

Section terminology: Notifications, Connection points and pointcuts, facets

A tangent is a class that can be used to perform a subordinate action on the specified action of a specified position, where the specified position first has a selection range, which can be a creation object, a method call, a variable change, and so on, which are the connection points, and you can decide which part of the selection is the tangent point, Of course, the process of choice is implemented in the code, once the point is selected, once these points are run, such as calling a class, a method in the slice class will be called to complete other functions, the method called is the notification.

That

    • Facets: Classes
    • Tangent: The action connection point of a specified code that is concerned with a tangent: code actions that can be used as tangency points
    • Notification: Methods, additional methods at the tangent point
    • Connection point: Code action for Tangency

1, notice (Advice) 5 kinds of

The work of the facets is called notification

The notification defines what and when to use the slice, and in addition to describing the work to be done on the slice, the notification solves the issue of when to do the work.

According to the method before, after, before and after, the exception can be divided into

    • Before: Invoking a notification before a method call
    • After: The notification is invoked after the method is called, regardless of whether the method is executed successfully
    • After-returning: Call notification after the method executes successfully
    • After-throwing: Call notification after method throws an exception
    • Around: Method is called before and after the call executes a custom behavior

2, Connection point (Joinpoint)

A connection point is a point in the execution of an application that is able to insert a slice, which is exactly the timing of a program's operation, such as when a method is called, when an exception is thrown, or even when a field is modified. Slice code can take advantage of these points (timing) to insert into the normal process of the application and add new behaviors.

3. Tangent point (Pointcut)

A facet requires only a limited number of connection points to be notified, not all of the connection points, and the pointcut defines which of the connection points is the notification that needs to work.

The pointcut matches one or more connection points that the notification is going to weave into

We typically use explicit class and method names to specify these pointcuts

Simply put, Tangency is a collection of valid connection points relative to different notifications

4. Slice (Aspect)

A slice is a collection of notifications and pointcuts, and what does the notification decide? When? The tangent point determines where to place the notification.

5. Introduction (Introduction)

Introduction is a description of a process that we add a new method or property to an existing class. We can introduce a class as a notification to the class being notified, thus changing the class without changing the class being notified.

6. Weave in (Weaving)

The slice is woven into the target object in the specified connection point (that is, the notification is notified in the Pointcut notification method, the notification is introduced to the notified Class), and the parentheses always say that the method is added to the notification class, and that the added method is to weave in, after all, to create a proxy class for the notified class, adding methods to the Proxy On the face of it, the agent class adds a method, that is, weaving in.

Weaving is the process of applying facets to a target object to create a new proxy object. Facets are woven into the target object at the specified connection point.

Multiple points in the target object can be woven into.

By wrapping facets in the proxy class, spring weaves the facets into spring-managed beans at run time.

Compile -time-slices are woven into the target class when it is compiled.

• This method requires a special compiler.

· AspectJ's weaving compiler is the way in which the facets are woven in this manner.

class Load period -facets are woven into the target class when it is loaded into the JVM.

• This approach requires a special class loader (ClassLoader) that can enhance the byte code of the target class before the target class is introduced into the application.

· AspectJ 5 LTW (load-time weaving) supports the weaving of facets in this manner.

run time --facets are woven during a period during which the application is running.

• In general, when a facet is woven, the AOP container dynamically creates a proxy object for the target object.

· Spring AOP is the runtime that creates a proxy object dynamically for the target object to be woven into. When a method call is intercepted. The agent executes the tangent logic before invoking the target bean. Spring does not create a proxy object until the bean that needs to be proxied is applied.

Iii. Spring's support for AOP

1. Spring provides 4 distinct AOP support

    • Agent-based classic AOP;
    • @AspectJ annotation-driven facets;
    • Pure Pojo plane;
    • Injection-ASPECTJ facets (for spring versions).

The first 3 types are spring Agent-based AOP variants, so spring's support for AOP is limited to method interception . If the requirement for AOP exceeds the scope of a simple method interception (such as a constructor or attribute interception), consider implementing a facet in ASPECTJ and injecting the spring bean into the ASPECTJ slice using spring's Di (Dependency injection):

2. Spring only supports method connection points

AspectJ and JBoss also provide field and constructor access points in addition to method pointcuts. So, with spring alone, we can't build fine-grained notifications or use constructor connection points. In this case, we can use aspect to assist with spring AOP.

3. The tangent point specifies those connection points

We use the pointcut to select the connection point, and then the different notifications match the different pointcuts. In spring AOP, you need to use AspectJ's pointcut expression to define a pointcut.

4. ASPECTJ Pointcut indicator supported by Spring AOP

Arg () restricts the execution method of the connection point matching parameter to the specified type

@args () to restrict the execution of connection point matching parameters that are marked by the specified annotation

Execution () is used to match the execution method of the connection point

This () restricts the connection point matching the bean reference for the AOP proxy to a class of the specified type

Target () restricts the connection point to a class that matches the target object to the specified type

@target () restricts connection points to match specific execution objects that correspond to classes that have annotations of the specified type

Within () restricts connection points to match the specified type

@within () Restricts the connection point to match the type indicated by the specified annotation (when using spring AOP, the method is defined in the class labeled by the specified annotation)

@annotation limit match with specified callout connection point

Only the execution Pointcut indicator is a unique execution match, while the other pointcut indicators are used to limit the matching.

5. Write tangency points, which are finally added to the XML configuration file

Note: The path that is written is the path to the interface class, and the tangency point is in the interface class instead of the concrete implementation class.

does not focus on return types, does not focus on entry types, and introduces pointcuts when method execution

Execution (* Com.springination.springidol.Instrument.play (..))

Increase the limit-and qualify the package path for the tangent point (the latter is redundant?). )

Execution (* Com.springination.springidol.Instrument.play (..))

and within (com.springinaction.springidol.*)

After spring2.5, the bean can be qualified by ID

Execution (* Com.springination.springidol.Instrument.play (..))

and within (com.springinaction.springidol.*)

and Bean (Eddie)

Of course, you can also use the reverse operation

Execution (* Com.springination.springidol.Instrument.play (..))

and within (com.springinaction.springidol.*)

and Bean (Eddie)

and!bean (EDDIE2)

6. Declaring slices in XML

AOP configuration element Description

<aop:advisor> Defining an AOP notifier

<aop:after> define AOP Post notifications (regardless of whether the method being notified succeeds)

<aop:after-returning> Defining AOP after-returning Notifications

<aop:after-throwing> Define after-throwing Notifications

<aop:around> Defining AOP Surround Notifications

<aop:aspect> Defining facets

<aop:aspect-autoproxy> Enable @aspectj annotation-driven facets

<aop:before> defining AOP Pre-notifications

<aop:config> AOP configuration elements at the top level. Most of the <AOP:*> elements must be contained within the <aop:config> element

<aop:declare-parents> introduces an additional interface to the notified object and transparently implements

<aop:pointcut> Defining pointcuts

Spring Series Spring AOP programming for facets

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.