File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Should not use getBean() method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Should not use getBean() method ?" Watch "Should not use getBean() method ?" New topic

Should not use getBean() method ?

Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

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?

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Via autowiring. But they say "ideally".

[My Blog]
All roads lead to JavaRanch
David Newton

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 26, 2008
Posts: 270

Thanks guys for your replies..

I think i need to dig more to understand what you guys are saying..
I agree. Here's the link:
subject: Should not use getBean() method ?
It's not a secret anymore!