What does IOC in Spring mean and why use IOC instead of New to create an instance?

1. Background introduction

Spring  Framework is an open source Java / Java EE full-stack ( full-stack ) application framework , released under the Apache License 2.0 open source license agreement, and also has a ported version on the .NET platform. Originally developed by Rod Johnson and Juergen Hoeller among others. The Spring Framework provides an easy development approach that avoids numerous property files and helper classes that can clutter the underlying code.

The core concepts of spring are IOC and AOP

2. Knowledge analysis

What is IoC

  Ioc-Inversion of Control , that is, " inversion of control " , is not a technology, but a design idea. In Java development, Ioc means handing over the object you designed to the container control, rather than the traditional direct control inside your object.


                           

Through the inversion of control, the client does not need to actively create objects, but only needs to pass the IOC, which is created by the IOC.

The object created by the application is changed to be created by the IOC container, and the main body used to control the creation of the object has changed, which is the inversion of control

The object information required by the application is injected after the IOC container is created, that is to say, it depends on the container to inject, which is dependency injection

The expression is one thing, but the angle of description is different.

IOC, from the container's point of view, the container inversely controls the creation of application objects.

DI, from the application's point of view, the application needs to depend on the container to inject objects.


3. Frequently Asked Questions

1) The difference between IOC and DI

2)  xml configuration injection method

4. Solutions

Write clear solutions to common problems

1) The difference between IOC and DI

The key to understanding IoC is: who controls who, controls what, why is it reversed, and in what ways is it reversed.

Who controls who, who controls what

In traditional JavaSE programming, we create objects directly through new inside the object. The program actively creates dependent objects, and IoC has a special container to create these objects, that is, the IoC container creates these objects. Who controls whom? Of course the object is controlled by the IoC container; what? That is mainly to control the acquisition of external resources (including objects, files, etc.).

Why and in what ways

If there is a reverse, there is a forward. Traditional applications are actively controlled by ourselves in the object to directly obtain the dependent object, that is, forward rotation. On the other hand, the container helps to create and inject dependent objects. Why is it reversed? Because the container helps us find and inject dependent objects, the object only passively accepts dependent objects, so it is reversed. What has been reversed? The acquisition of dependent objects is reversed.

理解DI的关键:谁依赖谁,为什么需要依赖,谁注入谁,注入了什么

1.谁依赖谁?当然是应用程序依赖于IoC容器。 
2.
为什么需要依赖?应用程序需要IoC容器来提供对象需要的外部资源(包括对象,文件等)。 
3.
谁注入谁?很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象。 
4.
注入了什么?就是注入某个对象所需要的外部资源(包括对象、资源、变量数据)

2) xml配置注入方式

·         1.Set注入

·         2.构造器注入

·         3.静态工厂的方法注入

·         4.实例工厂的方法注入

5.编码实战


6.扩展思考

7.参考文献

https://www.cnblogs.com/xdp-gacl/p/4249939.html

https://blog.csdn.net/c513063286/article/details/71516886

https://blog.csdn.net/suzunshou/article/details/47149895

8.更多讨论

(1)  When are Beans generally created?

Generally, it is created when the program starts.

(2) The way to inject beans 

Generally there are:

setter injection    

Constructor injection     

interface injection  

Annotation injection

(3) Bean instantiation method

1.setter method

2. Constructor

3. Static Factory

4. Instance Factory

(4) How to use ref="" and value="" in the configuration file respectively

value: used to directly specify a constant value;

ref: used to specify a reference to a bean instance in the bean factory




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324610950&siteId=291194637