Announcement Announcement Module
No announcement yet.
call to get hold of the already instantiated ApplicationContext ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • call to get hold of the already instantiated ApplicationContext ?

    I am trying to access a Spring bean (defined in teh applicationContext.xml) in the service/business layer. The question is , how do you reach the application context?
    The Spring reference has example of loading the xml file directly. That approach (below)

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    LocationService locationService = (LocationService) context.getBean("locationService");

    I notice that the entire Container(including the sessionFactory, dataSource, etc.) gets instantiated each time I do the above I just want to get the context which was loaded in the ContextLoaderListener listener (loaded in web.xml as below) and call getBean to get to my beans.

    What is the call to get hold of the already instantiated ApplicationContext ? (There are multiple classes from where I need to get access to the various spring Beans.)

    P.S : This is GWT application

    Thank you,

  • #2
    In a webapp using Spring, the context is loaded by the listener you put in web.xml and should NEVER be instantiated programmatically. It is automatically created and set up at application deploy on the server. To access a bean, you should inject it in the bean which uses it (after all, that's what Dependency Injection is all about).

    If, for some weird reason (other than the fact you didn't fully understand the Dependency Injection paradigm), you don't want to wire your beans directly, you can still obtain the application context in your bean programmatically by making its class implement the ApplicationContextAware interface.


    • #3
      Hi Enrico,

      Thanks for the information.

      I ended up resolving the issue by doing the following

      ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicati onContext(getServletContext());

      And since I was trying to get hold of the ApplicationContext from a Servlet(which becomes a service in GWT speak), I was able to get hold of the ServletContext using a getServletContext() call.

      Since I don't control the instantiation of a GWT ServiceImpl class, how would I be able to inject my BusinessService bean into such a class ?

      I appreciate knowing about ApplicationContextAware interface -- who knows where I might use it.



      • #4

        you didn't specify your service was a GWT servlet. The solution you came up with is perfectly feasible, but why should you re-invent the wheel while there already is a wide choice of middlewares for Spring-GWT integration that let you use Spring beans in GWT without resorting to getting the context programmatically every time? I suggest you take a look at this for a nice and clean solution, or just google "spring gwt integration" and choose the solution that best fits your needs.