• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 12012
257
  • 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: 12012
257
  • 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: 12012
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with just calling the constructor?
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic