This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependency Injection on a dynamically created object?

 
Paul Perret
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have some code that dynamically creates object instances according to the name of the class that's passed in (it creates different handler objects based on the type of message that's received).




Those handler objects (myObj) could use dependency injection to choose between two different Database types that we use. Is there a way to use DI for those handler objects, if they're created on the fly like this?

Thanks for the help!
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, because Spring injects beans into other beans it knows about. Anytime you instantiate an object with new, Spring doesn't know about it.

Mark
 
Paul Perret
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark, that's what I figured, but wanted to check. I ended up doing the dependency injection on the parent object (which is a bean), then passing that down to the created object.

BTW, I was in your Spring class in New Orleans last week. I thoroughly enjoyed it, and have already started using it a ton this week.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic