NOTE

What It Is

1. Explanation of IoC 2. Implementation principle 3. Benefits of IoC

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Explanation of IoC

Inversion of Control.

Before IoC, when we wanted to use an object, we first had to create it ourselves with new.

With Spring, Spring creates it for us and puts it into the container. We obtain the object directly from the container and use it.

2. Implementation Principle

Factory pattern + reflection + XML parsing.

Scan all XML files, parse them with dom4j, then create Beans and put them into a map in the factory. When we call getBean, the Bean is obtained from that map.

3. Benefits of IoC

Everything is for decoupling.

If A uses B and directly creates B with new, A and B are highly coupled.

After introducing a factory, the factory and B become highly coupled.

After introducing XML parsing, the factory class uses dom4j to parse XML and then uses reflection to create objects.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub