• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

what is the difference between DI and Spring IOC? or both is same?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Is there any difference between IOC and DI or both are same?

Regards,
Prabhat
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not the same but they tend to be described together, hence the confusion.

Inversion of Control (IoC) means that objects do not construct other objects on which they rely on. Instead, the application will get these objects from an external framework (an IoC container). One method which provides these objects to the application is called Dependency Injection (DI).
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IoC really means that code that doesn't have business logic drives code that contains business logic. A simple example is Servlet:- Technically, the way web container loads and uses servlets is IoC (even though the concepts of Servlets predates popular use of the term) The web container that contains logic for serving HTTP requests is calling the servlet. If you would do this in a non IoC way then you would have a HTTP Parser class and some sort of socket communication class that your servlet will call to listen for HTTP requests. Doing it the non-IoC also means that the generic code is not a container so it can't provide functionality like thread management, etc. Technically, you can have IoC without DI. The servlet container doesn't have DI.. but it does IoC


Technically, you can have DI without IoC, too. DI basically means that there is a module that is responsible for initializing all objects in the system. ALso, when objects are dependent on other objects, the DI module will give reference of objects to objects:- basically this process is called injection of dependencies. If A depends on B, the DI module will initialize A, initialize B, and give the reference of B to A:- the DI module has injected B into A. Now, technically, you could implement your own module that has intimate knowledge of the structure of your program. You can call the DI module from your init/main method and all of your objects will be created. Techncially, this is still DI, just not a very good one, because all the rules for constructing the objects is "hardcoded"

IoC and DI just maike sense together. IoC needs some sort of factory pattern, and using DI solves a lot of problems.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!