Announcement Announcement Module
Collapse
No announcement yet.
AppContext in Web App and standalone. how? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AppContext in Web App and standalone. how?

    Hi,

    I currently have a web application that uses Spring to create some servlet filters and associated beans in a declaritive fashion. I now need to expose one of the beans through an API I've created for extensions to the application. Esentially this would be a factory on the API.

    First problem is how can the factory class get an instance of the bean from Spring. I cant inject the bean as the factory isnt created by Spring (would require the user to retrieve it through spring).

    I could use something like this:

    myBean = WebApplicationContextUtils.getWebApplicationContex t(servletRequest).getBean("myBean");
    assuming I could get the ServletContext.
    This approach certainly wont work when I need to use this llibrary/API outside of the web container.

    Really seems like Spring is really missing the ApplicationContext/BeanFactory Factory. Also it seems odd that there is not a way to have a singleton ApplicationContext/BeanFactory automatically with Spring. One little static method somewhere and I would be set.

    Thanks for any pointers.

    Matt

  • #2
    Take a look at:

    http://www.springframework.org/docs/...ryLocator.html

    And heed its warnings about overexcessive use of JVM singletons. It can be a great tool, especially to link it as a parent to a WebApplicationContext defined in the web.xml. But obviously use it with care and discretion, and use DI everywhere you can.

    Comment


    • #3
      Thanks for pointing out the Singleton Bean Factory. I guess this still dosent quite solve my problem as I would like something that would still allow me to use the WebApplicationContext when in a web container and then any other ApplicationContext implementation when not in a web container.

      Ideally my code that needs to create beans programatically would just do something like:

      ApplicationContext.getInstance().getBean("myBean") ;

      For the web container the WebApplicationContext would be resolved and used. Outside a web app container it would resolve any other standard ApplicationContext that had been initialized.

      Again DI is great but I cant inject into applications that are yet to be written against my library. Thanks.

      Matt

      Comment


      • #4
        Hello,

        have a look at http://static.springframework.org/sp...ryLocator.html

        In a webApp you can create a WebApplicationContext that is a parent of the context found with the ContextSingletonBeanFactoryLocator. (So you can also add some web specific beans in the webApplicationContext...)

        If you need I can show you some code example tomorrow, I have to leave know

        Comment


        • #5
          Right. What pgras was saying. This means that you should only put web-related beans into your WebApplicationContext, and all other beans into the parent ApplicationContext. That way, a standalone app would never need to reference any beans in the WebApplicationContext.

          For the web project, you can define a servlet context initial parameter called parentContextKey which specifies the bean ref context ID to use as your parent application context class for your WebApplicationContext. Spring's ContextLoaderListener will do the rest for you.

          Comment

          Working...
          X