Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should not use getBean() method ?

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through the Spring 3.0 reference documentation and i found the following line:

You use getBean to retrieve instances of your beans. The ApplicationContext interface has a few other methods for retrieving beans, but ideally your application code should never use them. Indeed, your application code should have no calls to the getBean method at all, and thus no dependency on Spring APIs at all.


It is mentioned in the section 3.2.3 Using the container

So, If we dont use getBean() method at all,how can we get the beans into our code?

Thanks
Sudhakar
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Via autowiring. But they say "ideally".
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Via dependency injection/IoC (whether or not you're using autowiring).

Whether or not you'll have to use getBean depends on the app--if you're using a framework you might never make a call to getBean yourself--but the framework might. If you're using annotations, you might never even need to do that.
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your replies..

I think i need to dig more to understand what you guys are saying..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic