Spring Core Module, Spring IOC Tutorial

Core Module is the heart of Spring, tight coupling and loose coupling is the heart concept of Core Module. We are going to learn about to tight and loose coupling between java object in spring.

Tight Coupling Between Java Objects

  • In the above example, Run object is depends on car object. So run class creating an object of Car class
  • If the other class object is created in the dependent class [ like Car class object in run class ], there exist tight coupling, i mean if method in car object is changed then we need to do the changes in the Run class too so its the tight coupling between Run and Car class objects

Loose Coupling Between Java Objects

Let us see loose coupling between java objects.

In order to over come tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO model and through dependency injectionits possible to achieve loose coupling

In the above example Run , Car are tightly coupled. If we want to achieve loose coupling between the objects Run and Car, we need to re-write the application like

program............

In above example, spring container will inject either Car object or Bike object into the Run by calling setter method, So if Car object is replaced with Bike then no changes are required in Traveler class, this means there is loose coupling between Run and Vehicle object. Actually setter method will be activated through xml file.