Implementation principles of the Spring framework IOC and AOP

Source: Internet
Author: User

IoC (inversion of Control)

(1). IoC (inversion of Control) refers to the relationship between the container control program objects and not the traditional implementation, which is manipulated directly by the program Code. Control is transferred from the application code to the external container, and the transfer of control is called Inversion. For spring, It is the spring that controls the relationship between the Object's life cycle and the object, and the IOC has another name, "dependency injection (Dependency injection)." It is understood from the name that the so-called dependency injection, that is, the dependencies between components is determined by the container at run time, that is, the container dynamically injects some kind of dependency into the Component.

(2). in the way spring works, all classes are registered in the spring container, tell spring what it is, what you need, and spring will give you what you want when the system runs to the right time, and also give you what you Need. All classes are created and destroyed by spring, which means that the object that controls the lifetime of the object is no longer a reference to it, but Spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control Inversion.

(3). while the system is running, dynamically provide other objects that it requires to an object.

(4). the idea of dependency injection is implemented through a reflection mechanism, which, when instantiating a class, injects class attributes that were previously saved in HashMap into the class by reflecting the set method in the call class. All in all, in the traditional way of object creation, the caller is usually created to create an instance of the callee, and the work of creating the callee in spring is done by spring, and then injected into the caller, the so-called dependency injection or control Inversion. There are two ways to inject: dependency injection and set injection; the advantages of the ioc: reducing the coupling between components, reducing the complexity of replacements between business objects, and enabling them to manage objects Flexibly.

AOP (Aspect oriented Programming)

(1). AOP aspect-oriented programming is based on IOC and is a useful complement to oop;

(2). AOP uses a technique called "crosscutting" that splits the encapsulated object interior and encapsulates public behavior that affects multiple classes into a reusable module, named "Aspect", which is the Facet. The so-called "aspect", in a nutshell, is to encapsulate the logic or responsibility that is not related to the business, but for the common invocation of the business module, such as logging, to reduce duplication of code in the system, to reduce the coupling between modules, and to facilitate future operability and Maintainability.

(3). AOP represents a horizontal relationship that compares an "object" to a hollow cylinder, which encapsulates the Object's properties and behavior, and the Aspect-oriented programming method is to cut the cylinder open in a tangent plane and selectively provide the business Logic. The cut-off aspect is the so-called "facet". Then it hands the cut-off slices with a clever capture of the heavens, leaving no traces, but accomplishing the Effect.

(4). the implementation of AOP technology, mainly divided into two major categories: first, the use of dynamic agent technology, the use of interception of messages to decorate the message to replace the original object behavior, the second is the use of static weaving, the introduction of a specific syntax to create "aspect", so that the compiler can be in the compilation of the " Aspect "of the Code.

(5). Spring implements the AOP:JDK dynamic agent and the Cglib Agent JDK dynamic proxy: its proxy object must be an implementation of an interface that completes the proxy for the target object by creating an implementation class of an interface during runtime Two of its core classes are Invocationhandler and Proxies. Cglib Proxy: The implementation principle is similar to the JDK dynamic proxy, except that the proxy object that it generates during runtime is a subclass of the target class Extension.     Cglib is an efficient code-generation package that relies on ASM (open source Java bytecode editing class Library) to operate bytecode implementations, which are stronger than jdk, and need to introduce package Asm.jar and cglib.jar. Facets driven with ASPECTJ injection facets and @aspectj annotations are actually implemented by dynamic Proxies.

(6). AOP Usage scenarios:

Authentication permission Check

Caching Cache

Context Passing content delivery

Error handling Errors Handling

Lazy Loading Delay loading

Debugging commissioning

logging, tracing, profiling and monitoring logging, tracking, optimization, Calibration

Performance Optimization performance optimization, efficiency check

Persistence persistence

Resource Pooling resource Pool

Synchronization synchronization

Transactions Transaction Management

In addition, the implementation of filter and the implementation of STRUTS2 interceptors are the embodiment of AOP thought.

Implementation principles of the Spring framework IOC and 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: 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.