IOC and DI in Spring

Getting to know IOC and DI

At the beginning of learning Spring, IOC and DI will appear in front of us constantly. By using IOC and DI, the coupling of the program can be greatly reduced, and all objects can be managed through the container, which can solve some unnecessary problems. Trouble, here is a simple example: the database used in the project at the beginning was Mysql, so we used new Mysql() in the program, but after a few days we found that Mysql was not suitable, we had to change it to Oracle, and then we had to change The code of the program changes all about Mysql to Oracle. If you use IOC and DI, the source code does not need to be changed. You can easily solve it by adding an implementation class and modifying the XML configuration file. Therefore, to a large extent, IOC and DI have greatly liberated our program control, and all are managed by a container.

For IOC and DI, it is not a kind of technology, but a kind of thought.

IOC (Inversion of Control, inversion of control), is about handing over the designed object to the container for management, instead of manually controlling it in the object. In layman's terms, in an XML configuration file, configure the object's name, attributes, etc., and then create it by the container itself through a program. In fact, IOC is not a technology, but a kind of thought, which manages all kinds of unified management, and the basic basis for its realization is reflection. And DI (Dependency Injection, Dependency Injection), that is, after the calling right of the object is handed over to the container through IOC, the container configures the object and performs operations such as setting attributes. Therefore, IOC and DI are a complementary partner, and both are indispensable.

The core code of IOC

 

Preliminary IOC and DI

There are many concepts of IOC and DI on the Internet, so I won’t say much here, and use the simple implementation of IOC and DI to understand the specific implementation of IOC and DI.

The purpose of IOC and DI is to instantiate and initialize objects. So we need a configuration file to tell us which objects need to be handed over to the container for management. Now there are two common ways-Xml configuration and JavaBean annotation. Here we use the Xml way to achieve. In Xml, it is necessary to add the unique identifier (ID or Name) of the object to be managed, the fully qualified name, and the attributes of the object.

Configuration of Managed Objects

With the configuration object in the above figure, our primary goal now is to read the content of this configuration file. Here we use dom4j to read Xml. First, we create a ClassPathXmlApplicationContext object to manage the container. It implements a Custom ApplicationContext interface, this interface contains a method to obtain objects from the container. Specifically, as shown in the figure below, the beans in this object are the Map collection where the object is stored, and all our objects are placed in it. It can be seen that we have implemented the three operations of reading the configuration file, IOC, and DI in the construction method of this object, and then we can directly obtain the specified object through the getBean method.

Implement container objects by yourself to implement IOC and DI operations

After reading the information in the configuration file, we need to start creating objects based on this information. First, obtain the content of the bean tag in the configuration file through XPath. The bean tag must contain a unique identifier (ID or name) and a fully qualified name. These two parameters are indispensable and are the foundation of subsequent operations. Before generating an object, first judge whether the object is created repeatedly to avoid wasting space, then initialize the object through reflection + fully qualified name, and then put it into the map collection, with the ID as the key and the specific object as the value.

IOC implementation

Next, assign the initialized object, that is, DI. First get all the property tags under the bean node, because this tag is the property name and property value of the stored object, then get the Obj object according to the ID, loop through the data of the read property tag, and then fill in the properties according to the tag The class object of the name and Obj gets the PropertyDescriptor (property descriptor), and the set method of the corresponding property is obtained through the property descriptor (so our object must have a Get/Set method), and then according to the type of the property, the value stored in the property is transferred to For the specific type, then call the method's Invoke to set it.

DI implementation

The above is a basic implementation idea of ​​IOC and DI.

Guess you like

Origin blog.csdn.net/qq_35363507/article/details/104674522