The implementation principles of the Spring framework IOC and AOP for interview

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 of the execution, the second is the use of static weaving , the introduction of a specific syntax to create "aspects", This allows the compiler to weave code about "aspect" during compilation.

(5). Spring Implements AOP: TheJDK Dynamic Agent and the cglib Agent JDK Dynamic Agent: 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 .

The implementation principles of the Spring framework IOC and AOP for interview

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.