Newsletter

Spring Core Module, Spring IOC Tutorial

Spring » on Aug 4, 2011 { 71 Comments } By Sivateja

Core Module is the heart of Spring,Β  tight coupling and loose coupling is the heart concept of Core Module πŸ™‚Β  so let us try to understand about tight and loose coupling between java objects in spring [ you can’t move further with out understand this concepts, be care ]

Tight Coupling Between Java Objects

Let us see tight coupling between java objects first, take an example..

 

class Traveler
{
    Car c=new Car();
    void startJourney()
    {
       c.move();
    }
}

class Car
{
  void move()
  {
     // logic...
  }
}
  • In the above example, Traveler object is depends on car object.Β  So traveler class creating an object of Car class inside it [ line number 3 ]
  • If the other class object is created in the dependent class [ like Car class object in Traveler class ], there exist tight coupling, i mean if method in car object is changed then we need to do the changes in the Traveler class too so its the tight coupling between Traveler and Car class objects

Loose Coupling Between Java Objects

Let us see loose coupling between java objects, take an example..

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

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

class Traveler
{
    Vehicle v;
    public void setV(Vehicle v)
    {
      this.v = v;
    }      

    void startJourney()
    {
       v.move();
    }
}

 

Interface Vehicle
{
   void move();
}

 

class Car implements Vehicle
{
    public void move()
    {
         // logic
    }
}

 

class Bike implements Vehicle
{
    public void move()
    {
         // logic
    }
}

In above example, spring container will inject either Car object or Bike object into the Traveler 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 Traveler and Vehicle object.Β  Actually setter method will be activated through xml file, will see this later for now we are good to go πŸ˜‰

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

71 Responses to “Spring Core Module, Spring IOC Tutorial”
  1. ajab singh mewada says:

    To lot of thanks for this website sirji,i got very well example & in simple way.
    actually,sir i want some mailing related example so,plz give response in my email.once again lot of thanks …………..

    ajab singh mewada
    ajabmewada@gmail.com

  2. java4s.com says:

    @ Ajab

    Hi Ajab singh, thanks for your feedback.
    We are giving posts randomly, moreover java mail in spring will be covered in J2EE module.

    You will get special mail update once we posted regarding this. Moreover please be in touch with our Email Newsletter and in Facebook/Twitter.

  3. gaurav says:

    Sir yurs tightly coulping example is very good ….Thanks but loosly coupled is not understandable …..plz mail easy example gauravsarna38@gmail.com

  4. Java4s says:

    @gaurav

    Its nothing…,
    Loose coupling is just taking Interface reference, hmmm we should not say like this but see..

    We have only one method move() in our interface, so we must override this method only in all our implemented classes, so 1 interface into N number of classes you can implement.

    Hope it will be help full to you.

  5. Rammohan says:

    superb.

    very very clear about spring dependency injection…very good source for beginners who are eager to learn spring framework.

  6. Rajagopal says:

    This is nice documentation
    We are waiting for Spring-mvc documentation.
    Please pls………………….

  7. Thanks sir for the nice and easy example.very gud web site

  8. Niraj says:

    Thanks sir for great example and explanations.

  9. Suman says:

    Thnaks Siva, for you are nice explanation

  10. krishna says:

    It is a very good website for those who are interested to learn the frameworks like hibernate,struts and Spring..

  11. KISHORE says:

    Hello Sir,

    Thanks for your nice website.

    I have a quick question on the above explained tightly coupling concept.

    What exactly you mean by changing the method “if method in car object is changed”, signature then we will have same change in spring also rite.

    Please let me know your thoughts.

    • Vijay says:

      Hi Kisore,

      Here it is not like changing the method signature.

      If you want to choose car to bike then you need to do the code changes.
      But in spring you can use @Qualifier annotation and add the required dependencies over there

      @Autowired
      @Qualifier("bike") //you may use what you want here like car,bus,train etc..
      Vehicle v

      Hope you will understand

  12. rajasekhar says:

    i learnt hibernate from your site, ulitmate examples and explanation

  13. david says:

    thank you for explanation .

  14. can you explain ajax fully i think so its not completed still it as lot of concepts remaining

  15. Good explanation. Very helpful in understanding the concept.
    Can you please explain schedulers and how to use them with Springs

  16. Hi sir
    This is very nice tutorial. Sir can you give any example of getting user info from database in Json format.
    Thanks in advance……………..

  17. Giribabu says:

    Hi Siva,
    This is very nice website for those who are facing trouble in learning spring.Please provide spring-mve module also.

  18. abs says:

    Thank you for detail with example.

  19. Hi Sir Actually My Doubt Is When You Are changing method name in the interface we have to change in our implemented class right?

    In The Tight Coupling Also you explained in same way right?

  20. I need to switch windows user using java can you tell me how it is?

  21. madhu says:

    please improve search button and give topic vice.this website very usefull very easy to learn each topic with examples.

  22. Sir yours tightly coupled example is very good…Thanks a lot. But loosly coupled is not understandable.. plz mail easy example sd.ali786@gmail.com. waiting for reply mail..!

  23. Hi Sir..
    In many Interviews, I was asking a question like – What is DI and Inversion of control in spring? diff between DI and IOC? Can u explain it a easy way..! – @sd.ali786@gmail.com

  24. Aishwarya says:

    Wow, well explained !

  25. Nag@VNK says:

    You are provided very basic and core modules of each and every topics. Greatly useful for java learners.

  26. Thanks so much Siva, very nice site For JAVA . Very Helpful for me.

  27. mani says:

    really great afford,i have tried a lot of examplr from spirng ,mvc..but no one one was working,now its working,every exampple from your site is working.

  28. raju says:

    Hi Sir..
    In many Interviews, I was asking a question like – What is DI and Inversion of control in spring? diff between DI and IOC? Can u explain it a easy way..!

  29. meghana says:

    Hi Sir..
    I am very confused about diff between DI and IOC? and also i faced the question in interviews? Can u explain it a easy way..

  30. Prabha says:

    Hi,
    For 3 + years candidate we can except which type of ques regarding spring and hibernate.Please do help for this asap.Lok forward to ur mail

  31. samba says:

    please solve my answer anybody.

    How to stop a mybean inherit to other bean…?

  32. ram says:

    well, In loosely coupling if we change the method move() then it becomes tight coupling right…….! Pls Answer ….

  33. Ahmad says:

    Could u please explain the loose and tight coupling with same example ,that will help in clear differentiation b/w both.The example which u quoted above doesn’t clarify much.

  34. Suraj says:

    Sweet and Simple… Great Job ..!
    This is what every programmer need to understand…. Simplicity

    Thanks a lot ..!!

  35. Rajeswar says:

    could you please explain the project architecture
    when i am facing the interviews the most common question is explain the project architecture

  36. gudlakalyan says:

    excellent explanation ,after finding this post i stopped searching about this topic. thank you a lot .

  37. mahesh says:

    Thanx u verry much sirji be’z dependency is not easy to understand for the beginner nice example

  38. veerachary says:

    Thanks u very much sir,after reading this i am very confident about coupling concept

  39. rockjames03 says:

    Where do we write main method??? in traveller class or implementing class???

  40. Ramya says:

    Awesome tutorial for beginners like me..the different colors u used in the tutorial made us feel light while learning..:):)cool tutorial…Thanks!!!

  41. Ashish says:

    Hi , Very Nice Tutorial.
    But i want more easy example of tight coupling as i didn’t clear yet with the given example of tight coupling. Could you please send me or discuss more here on tight coupling.
    Thanks!!!!!

  42. This is Really helpfull thanks

  43. Dharma says:

    Hi shiva i’m Dharma from Banglore.ya i did go throw into your website and its seems to be a good website for begginers, i think it will add some extra flavours to all whoever looking for become a Java Developer but i think you need to enhance your website a little bit accordingly.

  44. suresh says:

    this is really helpfull

  45. ramanareddy sagili says:

    hi java4s guys its good but plese clarify the doubts of the peoples then its more helpul to others thanks boss just two days back i heared this website its good.

  46. Susmit says:

    Hey, this is an excellent tutorial, I am from Middleware background, just trying to learn Spring, JAX-RS, Hibernate, Jackson, Mule integration… Good way to start my journey… Only suggestion I have here is to include a navigation panel to the website where we can click and directly go to a specific topic!

    Cheers,
    Susmit

  47. Mukund says:

    thank u sir, u given very easy and suitable examples with explanation,so it easy to understand . sir I want Mvc based mini project . if possible please try to provide support ..thanks onece again..

  48. Supriya says:

    Thank you for these notes.

  49. arun says:

    very simple and everyone understand it superbb…

  50. Gurudath says:

    Sir your web-site for spring is simply superb. Could you explain me in detail for Tight-Coupling. or send me in my mail (gurudathdattu0@gmail.com)

  51. Vishal thakur says:

    Great example.. Sir give that type of example for spring-mvc fully @anotation based hello-wold program.

  52. sureshkumar says:

    I can’t understand with this example. Please give me some other example.

  53. dnyaneshwar says:

    Hi sir,

    you written content is very helpful sir a lot thanks but sir i have little confusion.

    in the tight coupling when we change in the car class we need to change inside the traveller class also and
    when we go through loose coupling and we do changes inside the car class although we need to changes in the traveller class for calling that particular method so what is the difference between both please clarify it.
    i have clarification about the interface and setter method and constructor these are very important in the dependency injection there are very helpful for less object creativity and independency but i have little bit confuion about coupling.

    Thank you.

  54. Rajkumar says:

    Hi Sir,

    I am beginner in spring So I tried to understand loose coupling and tight coupling but It is not clear much.please give me more detail example od these topics of spring.

  55. Rajeev says:

    very good article (y)

  56. shrikant verma says:

    very nice examples sir loose coupling and tight coupling i gotted easily.
    thanks…

  57. RajeshKamble says:

    Nice. It’s very simple and explain in a very beautiful way.
    Anyone can understand this very easily.

  58. dileep reddy says:

    Dear Verma,
    its a very good opportunity to gain knowledge in a simple way. great.

  59. Hemanth says:

    Hi,

    In one of my interview, the interviewer asked the question in the 1st example which you have posted above.
    class Traveler
    {
    Car c=new Car();
    void startJourney()
    {
    c.move();
    }
    }

    The question was, since we need to access the method of the car object, we are creating it in the Traveller class. How is it tightly couple?

    • Pranay says:

      If we change anything in class car it is dependent to traveler class, because we are creating the class object in traveler class.

  60. Veera babu says:

    Wrong answer.
    Why bcoz if 2 implemented classes have same method then IOC container which class method will inject to dependent class

  61. sachin chavanke says:

    Can any one tell me… how can we write spring xml bean file for above loose coupling code?

  62. Raji S says:

    Thank you so much for your explanation..simply you explained..

  63. Prasad Yara says:

    Hi all,

    What all I can say about this? Just now (today) started and it is really seems an amazing site that I have ever found.

  64. sathya p.v says:

    Able to understand tight coupling, and loose coupling is not clear please share some other example sir.
    i am preparing for interview, can you share any documentation related to spring framework. your answers are accurate and up to the point clear sir, that why asking for your material.

  65. Thadeuse says:

    It’s very simple and explain in a very beautiful way.

  66. rekha says:

    Sir your website is very helpful for me.But i need the concept of CURD operations in Spring.

  67. Pranay says:

    Awesome:)

    Still I am waiting for MVC with clear explanation.

    Regards,
    Pranay

  68. Sonali says:

    Good Explanation, with easily understandable example.

  69. Ahamed Kabeer says:

    Thank you..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.