Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Dependency injection

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object d is getting created  with values received from B.



I trying to inject spring in legacy code.Below is code snippet

 
Saloon Keeper
Posts: 10389
221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setup doesn't really make sense to me.

Your A class has a dependency on D, but D has a dependency on B which is only available when you call the save() method on A.

I think you want either this:

Or this:

It's very hard to give you a good solution though, without knowing what A, B and D represent. It would be useful if you told us that, and where B comes from and why D needs to be injected by the IoC container.
 
sat kadam
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tephan van Hulst,

Q1) Second Scenario matches my question asked earlier.In legacy save(),Object is created with the commented line using b as reference.How can i replicate same in spring using DI?



 
Stephan van Hulst
Saloon Keeper
Posts: 10389
221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't, because you have a circular dependency. A depends on D, D depends on B, and B is only passed to A after it's created.

The new situation doesn't match the old one: Before, A didn't have a dependency on D. Only the save() method used D internally. Why did you make it a private field?
 
sat kadam
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1) how can i create D object in save() internally using b as parameter. How this can be converted in spring?

 
Stephan van Hulst
Saloon Keeper
Posts: 10389
221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with just calling the constructor?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!