4

Use Spring IoC with servlets defined in web.xml

 2 years ago
source link: https://www.codesd.com/item/use-spring-ioc-with-servlets-defined-in-web-xml.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Use Spring IoC with servlets defined in web.xml

advertisements

This may be a relatively straight-forward question that I just haven't been searching for correctly, but I'm trying to use the Spring IoC container to configure my servlets. I have some additional handlers (that are private data members) and such that I would like to be configured at runtime. Is it possible to do this?

Right now I have my web.xml loading the servlets correctly, however, the problem is I'm not sure how to wire those instances to the Spring IoC container, or alternatively, wire the instances generated from the IoC container to the servlet container.

In my web.xml file, I'm setting up the ContextLoaderListener

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

And also configuring the correct location for the beans context

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/beans.xml
  </param-value>
</context-param>

I also know that the beans.xml file is being parsed because I got several exceptions when I had typed things incorrectly.

Thoughts? Is there a better way to do this?


I think most folks looking to do what you are just use the Spring MVC framework. In that case, the is a Spring class, and you just implement "Controllers" (instead of actual servlets).

That said, it's completely possible to do exactly what you're looking for. Check out FrameworkServlet (you can extend that) or DelegatingFilterPorxy (you could write a 'DelegatingSevletProxy' using this class as an example).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK