Announcement Announcement Module
Collapse
No announcement yet.
newbie Question on getting beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie Question on getting beans

    I was going through the spring documentation and read this paragraph

    "indeed, your application code should have no calls to the
    getBean(String) method at all, and thus no dependency on Spring APIs at all."

    Now I am just new to springs and whatever examples i have seen of basic sprong tutorials, all of them use the getBean method to get the instance of the class represented by the bean.

    How appropriate is this and what is the workaround?

    As per my understanding as soon as u start the container by providing it with xml config files having the bean defs, the conatiner should automatically instantiate the necessary beans.

    But it seems till the time getBean is called the instance never gets created. Also a simple prog I made which was not using getBean never worked.. Will try to post the code.

  • #2
    There are 2 options:

    a) Use getBean only once in the same static method that loads the context. That is, use getBean just to access the first bean

    b) If you deploy an Spring app in a container such as a web container, you won't need to call getBean. Everything would be configured in xml files

    Comment


    • #3
      thx rober...
      so basically if spring app is running in some container like web container then there is no need for getBeans.. ok

      but if i am writing simple test code say a static void main and want to use beans defined in spring config file then I'll have to go by getBeans method approach...

      thx a lot for the quick reply

      Comment

      Working...
      X