IOC and AOP of Spring Basic Principles

一、IOC(Inversion Of Control)

In software engineering, inversion of control (IoC) is a programming principle. IoC inverts the flow control as compared to traditional control flow. In IoC, custom-written portions of a computer program receive the flow of control from a generic framework. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.(from wikipedia)

       Compared with the traditional control flow, the process of creating an object and obtaining an instance is handed over to the IOC container, which manages the life cycle of the object to achieve the purpose of reuse and decoupling.

       What is a reversal? The container helps to find and inject objects, and the object changes from active creation to passive dependence, which is called inversion in the traditional sense.

       The commonly used method is DI (Dependency Injection), which is dependency injection, and the other is DL (Dependency Lookup), because the former is used more often, and it is often misunderstood that IOC is DI.

inversion of control

  • DI (Dependency Injection)

       There are two ways: Setter way (by value) and constructor way (reference way). The dependency relationship between components is determined by the container during the runtime of the application system, that is, the container dynamically injects the target object instance of a certain dependency relationship into each associated component in the application system.

  • DL (Dependency Lookup)
           has two methods: Dependent Drag and Drop (DP) and Context Dependent Lookup (CDL). Relying on drag and drop needs to rely on xml to manage beans, and the context is searched from the container.

Interesting metaphor: men and women looking for a partner. Before IOC, it was like men and women falling in love freely. To make friends, you need to take the initiative to confess to dating...the process is controlled by yourself; after IOC, it is like registering as a member of a matchmaker website. They manage a lot of information about single men and women, and then they will push it to The type you like, if the object is not suitable, an exception will be thrown. We don't have to worry about the matchmaker system handling the whole process.

二、AOP(Aspect Oriented Programming)

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a “pointcut” specification, such as “log all function calls when the function’s name begins with ‘set’”. This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development.(from wikipedia)

       AOP technology is just the opposite. It uses a technique called "cross-cutting" to dissect the inside of the encapsulated object, and encapsulates those public behaviors that affect multiple classes into a reusable module, which is named " Aspect", that is, the aspect. The so-called "aspect" simply means that the logic or responsibility that is not related to the business but is commonly called by the business modules is encapsulated, so as to reduce the duplication of code in the system, reduce the coupling between modules, and facilitate future operability and maintainability.

  • crosscutting concerns

       Interception methods and processing methods

  • Aspect

       Abstraction over crosscutting concerns

  • Join Point

       Spring only supports method-type connection points, and the actual connection point can also be a field or a constructor

  • Entry point (PointCut)

       Definition of interception on join points

  • Advice

       The code that needs to be executed after intercepting the connection point is divided into five categories: before, after, afterThrowing, afterReturning, and around

  • target

       proxy target

  • Weave

       Applying the aspect to the object makes the process of creating the proxy object: enter the target method, weave around first, then before, when exiting the target method, first implant around, weave afterReturning, and finally weave after. And surround notification will affect afterThrowing, generally not used at the same time.

  • Introduction

       On the premise of not modifying the code, the introduction can dynamically add methods or fields to the class at runtime

Guess you like

Origin blog.csdn.net/CUG_ZG/article/details/87743258
Recommended