Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are IoC and AOP?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are IoC and AOP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is IoC and AOP".

First of all, let's make it clear: IoC & AOP was not put forward 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 reverse / reverse control. It is an idea, not a technical implementation. Describes the creation and management of Java development domain objects.

For example, the existing class A depends on class B

Traditional development method: often in class A, new a B object manually through the new keyword.

Using the IoC idea of development: not through the new keyword to create objects, but through the IoC container (Spring framework) to help us instantiate objects. Which object we need can go directly through the IoC container.

From the comparison of the above two development methods: we "lost a power" (the power to create and manage objects), and thus got a benefit (no longer have to consider a series of things such as object creation, management, etc.).

Why is it called control reversal?

Control: refers to the power to create (instantiate, manage) objects

Reverse: control is handed over to the external environment (Spring framework, IoC container)

What problem did IoC solve?

The idea of IoC is that the two parties are not dependent on each other, and the related resources are managed by a third-party container. What's the advantage of this?

Reduced coupling or dependence between objects

Resources become easier to manage; for example, you can easily implement a singleton if you provide it with a Spring container.

For example, an existing operation for User is developed using the two-tier structure of Service and Dao.

In the case of not using the idea of IoC, if the Service layer wants to use the concrete implementation of the Dao layer, it needs to manually new the concrete implementation class UserDaoImpl of IUserDao in UserServiceImpl through the new keyword (you can't new the interface class directly).

It's perfect, and it's possible, but let's imagine the following scenario:

A new requirement is suddenly received during the development process, and another concrete implementation class is developed for the IUserDao interface. Because the Server layer depends on the specific implementation of IUserDao, we need to modify the object of new in UserServiceImpl. If only one class references the specific implementation of IUserDao, it may feel good, and it doesn't take much effort to modify, but if there are so many places that refer to the specific implementation of IUserDao, once you need to change the implementation of IUserDao, it will be a headache to modify.

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

IoC and DI, stop being silly and confused.

IoC (Inverse of Control: inversion of Control) is a kind of design idea or some kind of pattern. The design idea is to hand over the control of the manually created objects in the program to the Spring framework. IoC is also used in other languages and is not specific to Spring. IoC container is the carrier that Spring uses to implement IoC. IoC container is actually a Map (key,value), and various objects are stored in Map.

The most common and reasonable way to implement IoC is called Dependency Injection (DI).

It means that IoC is too common and unintentional, and many people will be confused by it, so it is better to use DI to name precisely.

What is AOP?

AOP:Aspect oriented programming is 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, and Dog, all of which have eat and run methods.

Through the inheritance in the idea of OOP, 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 get the eat () and run () methods by inheriting the Animal class. This will reduce a lot of repetitive code.

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

/ * *

* parent species of animals

, /

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 duration:" + (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 duration:" + (System.currentTimeMillis ()-start) / 1000F + "s")

}

}

This part of the repeated code is generally referred to as crosscutting logic code.

Problems with crosscutting logic code:

Code duplication problem

Crosscutting logic code and business code are mixed together, and the code is bloated and difficult to maintain.

AOP is used to solve these problems.

AOP puts forward a horizontal extraction mechanism to separate the crosscutting logic code from the business logic code.

Code splitting is relatively easy, but the difficulty is how to quietly apply the horizontal logic code to the original business logic without changing the original business logic, so as to achieve the same effect as the original.

What problem did AOP solve?

Through the above analysis, we can find that AOP is mainly used to solve: without changing the original business logic, enhance the crosscutting logic code, fundamentally decouple, and avoid crosscutting logic code duplication.

Why is AOP called aspect-oriented programming

Cut: refers to crosscutting logic, the original business logic code does not move, can only operate crosscutting logic code, so it is oriented to crosscutting logic.

Face: crosscutting logic code often affects many methods, each method is like a point, multiple points form a face.

Thank you for your reading, the above is the content of "what are IoC and AOP". After the study of this article, I believe you have a deeper understanding of what IoC and AOP are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report