Search code examples
tomcatpicocontainer

using picocontainer with tomcat servlets


I'm trying to use PicoContainer in a servlet based app running on Tomcat. Configuration apparently works in debug but unless as I supposed Servlet classes are not injected by construnctor and neighter by setter. Aren't servlet classes supposed to be injected? If not, how can I use the container in a servlet based app?


Solution

  • There's no IoC container that actually injects the servlets. They're instantiated by tomcat in your case. Typical Pico components are controllers, DAOs, business logic stuff etc.

    You can actually implement your web-app in two ways:

    1) put pico filter in play: your Controllers should be request scope components implementing Lifecycle interface. So such component will have request injected and then start() called by the container. Actually in this approach you will not have servlets at all. Probably makes sense to have single Router controller in request scope which will delegate specific url processing to other container components.

    2) as you said, ScopedContainers hides containers for external code, so you have to write your own PicoServletContainerListener (just take existing source and make scoped containers available to your code) in this method you can write usual servlets and delegate then to pico components