I recently started learning spring, started reading and met with bean, ioc and di, I don’t quite understand how they work, I have ready-made spring programs I wrote myself, but how ioc worked there I don’t understand what the difference is normal class and what is it?) can you please explain with simple examples? I will be glad to all the answers, thank you very much
-
https://www.theserverside.com/feature/Meaning-of-inversion-of-control-in-Spring-and-Java-IoC-explained – Alan Hay Feb 21 '19 at 14:09
-
3Possible duplicate of [What is Dependency Injection and Inversion of Control in Spring Framework?](https://stackoverflow.com/questions/9403155/what-is-dependency-injection-and-inversion-of-control-in-spring-framework) – Sagar P. Ghagare Feb 21 '19 at 14:48
1 Answers
Here goes a quick explanation, as you have already made an application. This is in the context of a Spring app, because those 3 concepts apply differently depending on the framework/context you're in.
IOC is Inversion Of Control. It means that the application won't manage it's lifecycle/flow of control itself. The framework (Spring) will. So you just tell the framework how you want (some) elements of your app to work together.
DI is dependency injection. It's a specific kind of IOC where the framework will manage the dependencies that an object uses (you can call dependency: a service).
Bean is an object managed by the Framework.
Here is part of a applicationContext.xml:
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
It's going to use this file to instanciate the classes Foo
, Bar
and Baz
(IoC), and inject both instance of Bar
and Baz
into Foo
(DI). Those instance are thus Beans
, managed by Spring (IoC).
If you ever need a bean (a service), you'll have to ask the Framework (using something like context.getBean(foo)
): you're not supposed to do a new Foo()
anywhere. Spring keeps an internal map of all these instances.
Again, it's in the context of a Spring app. These definitions will slightly differ if you're on a JEE application, for instance.

- 2,273
- 1
- 20
- 33
-
I am new to spring. Can you tell why it is good to delegate objects management to spring and use IoC – Ali Mar 22 '21 at 11:28
-
1@Ali You can check this question which dwelves deeper into the advantages of IoC (whether it is spring, or any other framwork): https://softwareengineering.stackexchange.com/questions/131446/what-is-inversion-of-control-and-when-should-i-use-it – Asoub Mar 22 '21 at 13:41