aspose file tools*
The moose likes Spring and the fly likes any DI example for local variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "any DI example for local variable?" Watch "any DI example for local variable?" New topic
Author

any DI example for local variable?

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 240
I just start to learn spring and I see many tutorial just talking about DI for class member variable
e.g. http://www.tutorialspoint.com/spring/setter_based_dependency_injection.htm
, but how to do DI for local variable (inside method)? any example?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You don't do this and why would you want to? Can you give me a use case where you would need to do this?


[How To Ask Questions][Read before you PM me]
peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 240
Bill Gorder wrote:You don't do this and why would you want to? Can you give me a use case where you would need to do this?


No special reason, just see all example about DI is for member variable, maybe I do not understand why need to use DI and so ask this question. At first, I think in spring application, there should be no any NEW Class statement. this is wrong concept?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Spring only does DI in Spring managed beans. Typically these are Services or Repositories which usually make sense as singletons (only one bean with that name existing in a given application context). Not every object is a good candidate to be Spring managed. Local variables even in these beans are still created with the new operator. You usually (this is possible but I would not worry about that for now) do not create Spring managed beans with the new operator. You define your beans and Spring creates the objects and manages the bean lifecycles and dependencies. This explanation is a bit oversimplified but if you have more specific questions let me know.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: any DI example for local variable?