Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • 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: 10649
227
  • 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: 10649
227
  • 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: 10649
227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with just calling the constructor?
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!