IoC and AOP, which are the most frequently asked questions by interviewers in spring recruitment, haven't you figured it out yet?

This article will start the dialogue from the following questions and explain the IoC & AOP

  • What is IoC?

  • What problem does IoC solve?

  • Difference between IoC and DI?

  • What is AOP?

  • What problem does AOP solve?

  • Why is AOP called Aspect Programming?

First statement: IoC & AOP were not proposed by Spring. They actually existed before Spring, but they were more theoretical at that time. Spring implements these two ideas well at the technical level.

What is IoC

IoC (Inversion of control) Control Inversion/Inversion of Control. It is an idea not a technical implementation. Describes: the creation and management of objects in the Java development domain.

Example: Existing class A depends on class B

  • Traditional development method: often in class A, manually use the new keyword to create a new object of B

  • The development method using the IoC idea: The key is not to create objects through new, but to help us instantiate objects through the IoC container (Spring framework). Which object we need can be put directly from the IoC container.

From the comparison of the above two development methods: we "lose a power" (the power to create and manage objects), and thus also gain a benefit (no need to consider a series of things such as object creation and management)

Why is it called Inversion of Control?

Control: refers to the power of object creation (instantiation, management)

Inversion: Control is given to the external environment (Spring framework, IoC container)

Edit switch to center

Add image comments, no more than 140 words (optional)

What problem does IoC solve

The idea of ​​IoC is that the two parties do not depend on each other, and a third-party container manages related resources. What's the benefit of this?

  1. The degree of coupling or dependence between objects is reduced;

  2. Resources become easier to manage; for example you can easily implement a singleton with a Spring container provided.

For example: Now an operation for User is developed using the two-layer structure of Service and Dao

In the case of not using the IoC idea, if the Service layer wants to use the specific implementation of the Dao layer, it needs to manually new out the specific implementation class UserDaoImpl of IUserDao in UserServiceImpl through the new keyword (the interface class cannot be directly new).

Edit switch to center

Add image comments, no more than 140 words (optional)

Perfect, this way is also possible, but let's imagine the following scenario:

During the development process, a new requirement was suddenly received, and another concrete implementation class was developed for the IUserDao interface. Because the Server layer depends on the specific implementation of IUserDao, we need to modify the new object in UserServiceImpl. If there is only one class that references the specific implementation of IUserDao, it may feel good, and it is not very laborious to modify, but if there are many places that reference the specific implementation of IUserDao, once the implementation of IUserDao needs to be replaced, then It will be very painful to modify.

Edit switch to center

Add image comments, no more than 140 words (optional)

img

Using the idea of ​​IoC, we hand over the control (creation, management) of the object to the IoC container for management, and we can directly ask the IoC container when we use it.

Edit switch to center

Add image comments, no more than 140 words (optional)

img

IoC and DI don't be confused anymore

IoC (Inverse of Control: Inversion of Control) is a design idea or a certain pattern. This design idea is to hand over the control of objects that were created manually in the program to the Spring framework. IoC has applications in other languages ​​as well and is not specific to Spring. The IoC container is the carrier used by Spring to implement IoC. The IoC container is actually a Map (key, value), which stores various objects.

The most common and reasonable implementation of IoC is called Dependency Injection (DI).

In addition, Martin Fowler mentioned in an article that IoC was renamed DI. The original text is as follows, the original address: https://martinfowler.com/articles/injection.html.

Edit switch to center

Add image comments, no more than 140 words (optional)

img

The old horse probably means that IoC is too common and does not express, many people will be confused because of this, so it is better to use DI to precisely name this mode.

What is AOP

AOP: Aspect oriented programming Aspect-oriented programming, AOP is a continuation of OOP (object-oriented programming).

Let's first look at an example of OOP.

For example: there are three classes, Horse, Pig, Dog, and these three classes have two methods, eat and run.

Through the inheritance in the OOP idea, we can extract a parent class of Animal, and then put the eat and run methods into the parent class. Horse, Pig, and Dog can automatically obtain the eat() and run() methods by inheriting the Animal class. . This will reduce a lot of repetitive code.

Edit switch to center

Add image comments, no more than 140 words (optional)

img

OOP programming ideas can solve most of the code duplication problems. But there are some problems that cannot be dealt with. For example, if duplicate code appears in the same position of multiple methods in the parent class Animal, OOP cannot solve it.

 
 
 
 

/** * Animal parent class*/ public class Animal { /** height*/ private String height; /** weight*/ private double weight; public void eat() { // performance monitoring code long start = System.currentTimeMillis (); // Business logic code System.out.println("I can eat..."); // Performance monitoring code System.out.println("Execution time: " + (System.currentTimeMillis() - start) /1000f + "s"); } public void run() { // performance monitoring code long start = System.currentTimeMillis(); // business logic code System.out.println("I can run..."); // Performance monitoring code System.out.println("Execution time: " + (System.currentTimeMillis() - start)/1000f + "s"); } }

This part of repeated code is generally referred to as cross-cutting logic code.

Edit switch to center

Add image comments, no more than 140 words (optional)

img

Problems with cross-cutting logic code:

  • code duplication problem

  • Cross-cutting logic code and business code are mixed together, the code is bloated and maintained unchanged

AOP is used to solve these problems

AOP takes a different approach and proposes a horizontal extraction mechanism to separate the cross-cutting logic code from the business logic code

Edit switch to center

Add image comments, no more than 140 words (optional)

img

Code splitting is easier, but how to silently apply the horizontal logic code to the original business logic without changing the original business logic to achieve the same effect as before.

What problem does AOP solve

Through the above analysis, it can be found that AOP is mainly used to solve: without changing the original business logic, enhance the cross-cutting logic code, fundamentally decouple, and avoid cross-cutting logic code duplication.

Why is AOP called Aspect Oriented Programming?

Cut: refers to the cross-cutting logic, the original business logic code does not move, only the cross-cutting logic code can be operated, so it is oriented to the cross-cutting logic

Face: Cross-cutting logic code often affects many methods, each method is like a point, and multiple points form a face. There is a concept of face

Guess you like

Origin blog.csdn.net/wdjnb/article/details/124295153