• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

any DI example for local variable?

 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • 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
  • 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: 250
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic