Spring Combat (ix) AOP concepts and Spring AOP

Source: Internet
Author: User

1. Crosscutting concerns (cross-cutting concern)

In software development, the many functions scattered in the application are called crosscutting concerns, such as transactions, logs, and security.

Crosscutting concerns are conceptually separate from the business logic of the application (but tend to be embedded directly into the business logic of the application), and separating crosscutting concerns from business logic is the problem that AOP solves.

DI is used for decoupling between objects, and AOP enables decoupling between crosscutting concerns and the objects they affect.

2, reusing common functions, the most common technology is inheritance and delegation

Inheritance leads to fragile object architectures, and delegates may need to make complex calls to delegate objects.

So replace them with slices.

3. AOP Terminology

Aspect: Facets-Blocks the crosscutting concerns into special classes called facets.

Advice: Notification or enhancement--the function to enhance or increase, with before,after,after-returning,after-throwing,around

Join point: Connection points-All "points" (timing) that can be inserted into a slice during application execution

Pointcut: Tangent--in practice, select the connection point where the slice is inserted, which defines which points are enhanced.

Introduction: Introducing--adding new methods and properties to an existing class.

Weaving: Weaving--the process of applying facets to a target object and creating a new proxy object. (can also be implanted)

4. In the life cycle of an early target object, there are multiple points that can be woven into:

    • Compile time-A special compiler is required, and the ASPECTJ weaving compiler is woven into the plane in this way.
    • Class load period--facets are woven into the target class when it is loaded into the JVM, requiring a special class loader that enhances the target class's bytecode before the target class is imported into the application.
    • Run time-facets are woven at some point in the application's run. In general, when you weave a slice, the AOP container dynamically creates a proxy object for the target object, and Spring AOP weaves it in this way!

  

5.4 Types of AOP support are available in spring

    • Agent-based classic spring AOP;
    • Pure Pojo Facets--xml configuration
    • @AspectJ annotation-driven facets-no XML configuration required
    • Injection-type ASPECTJ facets

The first three are variants of spring AOP implementations, and spring AOP is built on dynamic proxies, so spring's support for AOP is limited to method interception. If your AOP needs exceed simple method invocations, such as constructors or attribute interception, then you need to consider using ASPECTJ to implement facets

6. The notifications created by spring are written in standard Java classes, and the pointcut that defines the notification application can be written in XML using annotations or in a spring configuration file.

ASPECTJ uses the unique AOP language to gain stronger and finer-grained control.

7. Spring notifies/enhances target object at runtime--dynamic agent

The facets in spring are implemented by the proxy class that wraps the target object. The proxy class intercepts and processes calls to the method, executes additional facet logic, and then invokes the method of the target object.

When was the proxy object created?

Spring creates a proxy object when the agent's bean is actually applied.

When is the object being proxied created?

If you use Appplicationcontext,appplicationcontext to load all beans from beanfactory, Spring creates the Proxied object.

8. Spring supports only method-level connection points.

There are several models of connection points in AOP, such as field and constructor access points.

However, spring only supports method connection points, so you cannot create fine-grained notifications.

Spring Combat (ix) AOP concepts and 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.