• Post Reply Bookmark Topic Watch Topic
  • New Topic

Spring IOC Container - ApplicationContext

 
Rahul Dazz Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every where over the internet, people are professing that IOC approach is better than non IOC approach as it decouple your classes from their dependencies so that the dependencies can be replaced or updated with minimal or no changes. Refer below java programs, one using the IOC approach (file 1, 3 and 4) and another without using the IOC approach (file 1 and 2).

File 1 - Test.java [POJO]

File 2 - Client.java [Without using IOC Container]

File 3 - Client.java [Using IOC Container: ApplicationContext]

File 4 - spring.xml

No matter whichever approach we follow, we will get the same result "Hello Mr. Rahul". Can anyone please guide as what benefits the IOC approach is offering (in context with the source code shared above) which non IOC approach isn't.
 
preetika shukla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,

Based on the example which you've given, you can see that Client.java(with out IOC) is very much aware about the type of the objects, which is creating (test here). Say down the line, if you were to have similar functionality for some other class, you will manually go ahead and make the changes in the code to create a new object of it.

Where as in the case of where you're using IOC concept, you can just go ahead change the spring.xml and make no changes in your already written/well tested code.

Also, cases where you're writing test cases for the method, you cannot mock the new object which is getting created inside the method (cases without IOC).

Hope this helps!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!