In-depth understanding of the two major characteristics of Spring (IOC and AOP)

As we all know, the core features of Spring are IOC and AOP, IOC (Inversion of Control), that is, "inversion of control"; AOP (Aspect-Oriented Programming), that is, "aspect-oriented programming". The reference book "Spring In Action", let me share my personal understanding of these two characteristics.

IOC: IOC, another term is called DI (Dependency Injection), that is, dependency injection. It is not a technical implementation, but a design idea. In any program project with practical development significance, we will use many classes to describe their unique functions, and complete specific business logic through the mutual cooperation between classes. At this time, each class needs to be responsible for managing the references and dependencies of the classes that interact with it, and the code will become extremely difficult to maintain and extremely highly coupled. The emergence of IOC is used to solve this problem. We pass the creation and coordination of these interdependent objects to the Spring container for processing. Each object only needs to pay attention to its own business logic relationship. From this point of view, the way to obtain dependent objects is reversed, and the spring container controls how objects obtain external resources (including other objects and files, etc.).

Example: One day, you were sick, but you didn't know what was wrong with you. All you knew was that you had a headache, cough, and general weakness. At this time, you decide to go to the pharmacy to buy medicine. There are many kinds of medicines in the pharmacy. There are dozens of kinds of medicines just for the treatment of headaches, and there are differences between Western medicines and Chinese medicines. Then you read the instructions yourself, choose a box of medicines that you feel can best treat your symptoms, pay for the medicines, and hope to get better soon. 
But this process, for a patient, is too hard. Headache, cough, general weakness, and I have to read the instructions of each medicine one by one, and compare which medicine is better one by one, it is simply too tiring. At this point, you decide to go straight to the hospital to see a doctor. 
The doctor has examined you to know what your condition is and what causes it; at the same time, the doctor knows very well what medicines can treat your pain, and can screen according to your own conditions. In just ten minutes, you can get the right medicine, which saves time and effort.

In the above example, the IOC plays the role of a doctor. It collects your needs and requests, and prescribes the medicine directly to you. You are the object, and the medicine is the external resource you need. Through the doctor, you don't have to go to the medicine, but the medicine is prescribed to you through the doctor. That's the essence of the whole IOC.

AOP: Aspect-Oriented Programming, often defined as a technique that enables software systems to achieve separation of concerns. A system is made up of many different components, each responsible for a specific piece of functionality. In addition to implementing their core functionality, these components often have additional responsibilities. Core services such as logging, transaction management, and security are often incorporated into components that have their own core business logic. These system services are often referred to as cross-cutting concerns because they span multiple components of the system.

The concept of AOP is not like an example of IOC. Now we will talk about what technology AOP is based on the specific implementation in a system.

We take the transaction management and control commonly used in the system as an example. In the process of operating the database system, it is inevitable to consider the content related to the transaction. If a new transaction manager is created in each method, it is undoubtedly a serious coupling and intrusion to the code. In order to simplify our development process (in fact, everything spring does is to simplify the development process), we need to extract the transaction-related code as an independent module. Through AOP, it is confirmed that each operation database method is a connection point, and these connection points form an aspect. When the program runs to one of the pointcuts, we weave the transaction management module into the object, and complete the realization of the entire transaction management and control through the notification function. In this way, all methods of operating the database do not need to care about the content of transaction management separately, but only need to pay attention to the implementation of their own business code. All transaction management related content is implemented through AOP. The content of the code is simplified, the complex content of the target object is decoupled, and the business logic and cross-cutting concerns are separated.

The following introduces the terms related to AOP:

  • Notifications: Notifications define the concept of what an aspect is and when to use it. Spring aspects can apply 5 types of advice:

    • Before notification (Before): The notification function is called before the target method is called.
    • After notification (After): Call notification after the target method is completed, and do not care what the output of the method is at this time.
    • After-returning: The notification is invoked after the target method has successfully executed.
    • Exception notification (After-throwing): The notification is invoked after the target method throws an exception.
    • Around advice (Around): The advice wraps the advised method, performing custom actions before and after the advised method is called.
  • Join Point: A point at which an aspect can be inserted during application execution.

  • Pointcut: A pointcut defines one or more join points where the aspect is to be woven.
  • Aspects: A combination of advice and pointcuts. Together, advice and pointcuts define what an aspect is all about.
  • Introduce: Introduce allows us to add new methods or properties to an existing class.
  • Weaving: is the process of applying aspects to target objects and creating new proxy objects. Aspects are woven into the target object at specified join points. There are multiple points in the target object's lifecycle where weaving can take place: 
    • Compile time: Aspects are woven in when the target class is compiled. This way requires a special compiler. AspectJ's weaving compilers weave aspects in this way.
    • Class loading time: Aspects are woven in when the target is loaded into the JVM. This approach requires a special class loader that enhances the bytecode of the target class before it is introduced into the application.
    • Runtime: Aspects are woven in when the application runs to a certain point. Typically, when weaving an aspect, the AOP container dynamically creates a proxy object for the target object. Spring AOP is woven into aspects in this way.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325923959&siteId=291194637