The IOC simulation implementation of spring is introduced in detail

Simply put, when you need an object, you don't need to manually create a new one, but other containers will help you provide it; Spring is the IOC container.

For example:

In spring, it is often necessary to assemble a Dao in the service, generally using @ Autowired annotation: similar to the following

If you use DAO uninitialized directly here, null pointer exceptions will be reported. The way in spring is to load the required classes for you through reflection.

About IOC: let's tell a story!

There is a cook who needs some kind of bean when making a dish, but he doesn't have that bottle of bean. At this time, he must make a bottle of bean. (this is just like when we usually need objects: userdao userdao = new userdaoimpl) at this time, the chef's work has to be connected with making seasonings, which is the so-called "coupling". (in other words, I can't live without you.);

At this time, the control in IOC is reversed. It proposes to open up a warehouse (container) with various seasonings (beans). When you need a seasoning (bean), you can get it directly by giving the name of the seasoning (beanname). The code is similar to this: (userdao user = warehouse. Get ("seasoning name"); In this way, you can get the bean directly without waiting for you to make a bottle when you don't have it. This is the so-called transfer of control. It directly lets the warehouse (container) produce (produce) the work of producing seasoning (bean);

Next, when dependency injection comes out, it becomes more powerful, It proposes that as long as the chef has a bottle (private userdao userdao), opens the bottle cover (method: setuserdao (userdao userdao) {this. Userdao = userdao} that is, provides the set method for the), and then attaches a label to the bottle to indicate what material to put (configure < property name = "userdao", ref = "xxbean" in the XML file), then you don't have to do anything at this time, The warehouse (container) will automatically send someone to help you put the seasoning bean you need into the bottle; when you want to change another seasoning, just change the label on the bottle to something else, such as pepper (at this time, you need to change the bean of ref in the XML file). Depending on the injection, the flexibility of the code is improved. When you need to replace the implementation of the class, You don't need to modify it, just modify it in the XML configuration file;

To sum up, my conclusion: the main purpose of IOC is to realize decoupling! Decoupling! Decoupling! Important things are to be repeated for 3 times. From the chef's Liezi I'm a cook. I only cook. I don't make seasoning ~ ~ I'm not a seasoner. I don't have so much to do with making seasoning With the emergence of IOC, chefs only need to do their own things (cooking) at ease and take seasonings directly from the warehouse! In this way, the relationship between chefs and seasoning production will be separated, which is decoupling! Decoupling! Decoupling! So that the relationship between the two things is not so close

The main technology used to simulate the implementation of IOC is java reflection mechanism (the architecture used for simulation is divided into Dao layer, service layer and controller layer):

Note: you are welcome to give advice. I'm also learning. If you don't speak well, please forgive me~

I Write Dao classes to test:

II Write Dao implementation classes:

III Write the service class:

IV Write the implementation class of the service:

V Write bean XML configuration file (I here can be regarded as iocdaoimpl class, iocservice as iocdaoserviceimpl, iocdao is an attribute in iocdaoserviceimpl, and the parameter value passed in by this attribute is "I");

Vi Write factory interface

VII Write classpathxmlapplicationcontext to read the configuration file, and according to the bean XML configuration object to generate various beans and complete the injection work (the most important part is the implementation principle of IOC)

VIII Writing test classes

IX Display results:

summary

The above is all about the IOC simulation implementation of spring in this paper. I hope it will be helpful to you. Welcome to: spring's detailed explanation of IOC and Di, spring configuration scanning multiple packages, spring JDBC batch data processing code examples, etc. you can leave a message at any time. Welcome to exchange and discuss.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>