I threw together a quick and dirty ServletToBeanProxy. It doesn't have FilterToBeanProxy's 'targetClass' capability, but it does have 'targetBean'. This allows you to define servlets as beans in a Spring bean factory (application context) - providing all the benefits of dependency injection (along with full access to your Spring application context).
Code:
public class ServletToBeanProxy implements Servlet
{
private Servlet delegate;
protected ApplicationContext getContext(ServletContext servletContext)
{
return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}
public void init(final ServletConfig servletConfig) throws ServletException
{
final String targetBean = servletConfig.getInitParameter("targetBean");
final ApplicationContext ctx = getContext(servletConfig.getServletContext());
if(targetBean == null || !ctx.containsBean(targetBean)) {
throw new ServletException("targetBean '" + targetBean + "' not found in context.");
}
this.delegate = (Servlet)ctx.getBean(targetBean, Servlet.class);
this.delegate.init(servletConfig);
}
public ServletConfig getServletConfig()
{
return this.delegate.getServletConfig();
}
public void service(final ServletRequest servletRequest,
final ServletResponse servletResponse) throws ServletException,
IOException
{
this.delegate.service(servletRequest, servletResponse);
}
public String getServletInfo()
{
return this.delegate.getServletInfo();
}
public void destroy()
{
if(this.delegate != null) {
this.delegate.destroy();
this.delegate = null;
}
}
}
Pretty straight forward, as you can see. Here is a sample config from my web.xml:
Code:
<servlet>
<servlet-name>MqHttpTunnelServlet</servlet-name>
<servlet-class>...ServletToBeanProxy</servlet-class>
<init-param>
<param-name>targetBean</param-name>
<param-value>MqHttpTunnelServlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MqHttpTunnelServlet</servlet-name>
<url-pattern>/services/mq</url-pattern>
</servlet-mapping>
In this case, the ServletToBeanProxy will lookup a bean by the name of "MqHttpTunnelServlet" in my Spring WebApplicationContext and proxy all servlet functionality to that bean.
- Andy