Announcement Announcement Module
Collapse
No announcement yet.
Spring newbie - going nutso about the use of getBean() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring newbie - going nutso about the use of getBean()

    I have been reading many posts the past few days about whether you should use getBean() or not from your bean factory / application context. However I still have not seen any concrete example (or maybe my eyes just aren't seeing it) on how to avoid using getBean() from an app context / bean factory for at least one object. I have seen many examples of using Service Locators and other strategies but inevitably when they show the beans.xml or whatever they call it I never see how they get that one lonely bean into their application.

    Quick background: Our app is a Java Swing / Command line application (no web stuff here). I would like to convert a large portion of our configuration base over to Spring IoC and move the coupling from inside the code to the configuration xml.

    To start I will simply have a set of ContextGroups that will contain various Contexts within them. Other Pojo ojects will belong to these Contexts.

    In this case my top level (container) object(s) are the set of ContextGroups. I see how to set up and build the bean definitions in the xml to mend everything together to produce this relationship but then there is still the issue of having my main() get a reference to one or more of these ContextGroups without doing beanFactory.getBean("myGroup");

    I don't have a problem using one or two getBean() calls in my application. I figure I may have to as I am introducing Spring late into the architecture. However my curiosity and desire to learn is forcing me to ask : "How do you wire up your application to avoid calling getBean() at least once in your code"?

    thanks in advance

  • #2
    In these types of applications you will find that it is common to use getBean(..). In this situation spring is acting as a service locator (those are the types of objects that you should be having spring constructing and injecting).

    In J2EE applications using controllers and MDP's, etc, all of the business components can be constructed and injected into the framework objects by the spring container and it is very rare that you NEED to manually access the context and make getBean(...) invocations.

    Comment

    Working...
    X