• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

any DI example for local variable?

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
peter tong
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic