aspose file tools*
The moose likes Spring and the fly likes Dependency Injection on a dynamically created object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dependency Injection on a dynamically created object?" Watch "Dependency Injection on a dynamically created object?" New topic
Author

Dependency Injection on a dynamically created object?

Paul Perret
Greenhorn

Joined: May 18, 2011
Posts: 2
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Paul Perret
Greenhorn

Joined: May 18, 2011
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency Injection on a dynamically created object?
 
Similar Threads
Dependency Injection with setter injection ?
How do I add a object (copy) to a que or hashmap, and not a reference?
What is dependency injection?
Confusion over intern of String class
From main method to contractor, it is possible?