Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this works

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
a.setB(b);
b.setA(a);

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

Mark
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep! That Helps

Thank You
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic