Newsletter |
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 πΒ 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 .:: | ||
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
@ 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.
Sir yurs tightly coulping example is very good ….Thanks but loosly coupled is not understandable …..plz mail easy example gauravsarna38@gmail.com
@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.
superb.
very very clear about spring dependency injection…very good source for beginners who are eager to learn spring framework.
This is nice documentation
We are waiting for Spring-mvc documentation.
Please pls………………….
Thanks sir for the nice and easy example.very gud web site
Thanks sir for great example and explanations.
Thnaks Siva, for you are nice explanation
It is a very good website for those who are interested to learn the frameworks like hibernate,struts and Spring..
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.
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
i learnt hibernate from your site, ulitmate examples and explanation
thank you for explanation .
can you explain ajax fully i think so its not completed still it as lot of concepts remaining
Good explanation. Very helpful in understanding the concept.
Can you please explain schedulers and how to use them with Springs
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……………..
Hi Siva,
This is very nice website for those who are facing trouble in learning spring.Please provide spring-mve module also.
Thank you for detail with example.
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?
I need to switch windows user using java can you tell me how it is?
please improve search button and give topic vice.this website very usefull very easy to learn each topic with examples.
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..!
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
Wow, well explained !
You are provided very basic and core modules of each and every topics. Greatly useful for java learners.
Thanks so much Siva, very nice site For JAVA . Very Helpful for me.
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.
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..!
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..
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
please solve my answer anybody.
How to stop a mybean inherit to other bean…?
well, In loosely coupling if we change the method move() then it becomes tight coupling right…….! Pls Answer ….
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.
Sweet and Simple… Great Job ..!
This is what every programmer need to understand…. Simplicity
Thanks a lot ..!!
could you please explain the project architecture
when i am facing the interviews the most common question is explain the project architecture
excellent explanation ,after finding this post i stopped searching about this topic. thank you a lot .
Thanx u verry much sirji be’z dependency is not easy to understand for the beginner nice example
Thanks u very much sir,after reading this i am very confident about coupling concept
Where do we write main method??? in traveller class or implementing class???
Awesome tutorial for beginners like me..the different colors u used in the tutorial made us feel light while learning..:):)cool tutorial…Thanks!!!
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!!!!!
This is Really helpfull thanks
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.
this is really helpfull
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.
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
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..
Thank you for these notes.
very simple and everyone understand it superbb…
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)
Great example.. Sir give that type of example for spring-mvc fully @anotation based hello-wold program.
I can’t understand with this example. Please give me some other example.
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.
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.
very good article (y)
very nice examples sir loose coupling and tight coupling i gotted easily.
thanks…
Nice. It’s very simple and explain in a very beautiful way.
Anyone can understand this very easily.
Dear Verma,
its a very good opportunity to gain knowledge in a simple way. great.
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?
If we change anything in class car it is dependent to traveler class, because we are creating the class object in traveler class.
Wrong answer.
Why bcoz if 2 implemented classes have same method then IOC container which class method will inject to dependent class
Can any one tell me… how can we write spring xml bean file for above loose coupling code?
Thank you so much for your explanation..simply you explained..
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.
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.
Itβs very simple and explain in a very beautiful way.
Sir your website is very helpful for me.But i need the concept of CURD operations in Spring.
Awesome:)
Still I am waiting for MVC with clear explanation.
Regards,
Pranay
Good Explanation, with easily understandable example.
Thank you..