File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes How does this works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How does this works" Watch "How does this works" New topic

How does this works

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

I'm learning a bit on Spring , so i didn't understand how this Execution happens because my A class object is depend on B class object , b instance is again depend on a ;

My beans.xml

My Manger Class

Can some one explain me , how DI happens here (Dependency Injection).
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

First, I recommend learning the latest version of Spring. The sample code you use BeanFactory instead of ApplicationContext. This means that you are using at least 5 year old material, as today to start up Spring you should create an ApplicationContext instead.

Now, in your case you changed your dependency to be property dependency. When you had constructor dependency, those dependencies are mandatory, but how can you create one before the other if they depend on each other both via constructor args. Just think in Java can you do

A a = new A(b);
B b = new B(a);

No because the first line doesn't work, there is no instance of B created yet, and if you tried to reverse the two lines, same problem but the other way.

But I can do

A a = new A();
B b = new B();

That works.

Now, I would take that circular dependency and see it as a code smell and refactor the code so that there is no circular dependency.

Hope that helps


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Yep! That Helps

Thank You
I agree. Here's the link:
subject: How does this works
It's not a secret anymore!