我有一个我无法修改的第三方servlet。它接受我想要外化的init-param
(来自web.xml)。
我可以使用DelegatingFilterProxy将servlet过滤器init-param
外部化。这有效地将servlet过滤器定义移动到Spring中,其中有更强大的外化工具(例如:PropertyPlaceholderConfigurer,环境变量等)。
如何为servlet执行此操作?
答案 0 :(得分:6)
看起来你需要一个DelegatingServletProxy
类,虽然这在Spring中不存在但是,我认为编码是非常简单的,使用DelegatingFilterProxy作为起点。
servlet只实现了一些具体的方法,因此委托应该是直截了当的。
好的,我把钱放在嘴边!
编辑:低于DelegatingServletProxy
的基本实现。
你这样使用它:
一些示例,在spring应用程序上下文中
<bean name="myInitParams" class="java.util.Properties">
<constructor-arg>
<props>
<prop key="initParamName">initParamValue</prop>
</props>
</constructor-arg>
</bean>
示例web.xml
代码段
<servlet>
<servlet-name>...</servlet-name>
<servlet-class>
acme.DelegatingServletProxy
</servlet-class>
<init-param>
<param-name>proxyServletClass</param-name>
<param-value>your.original.servlet.ServletClass</param-value>
</init-param>
<init-param>
<param-name>proxyServletParams</param-name>
<param-value>myServletParams</param-value>
<!-- name of bean in spring context -->
</init-param>
</servlet>
这是servlet代码,它很长,但大多数是委托ServletContext
- 有趣的东西发生在顶部。它未经测试 - 应该被视为一个起点。
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
public class DelegatingServletProxy extends HttpServlet implements WebApplicationContextAware
{
private HttpServlet delegate;
private Properties initParams;
private String delegateName;
public void setDelegateName(String delegateName)
{
this.delegateName = delegateName;
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
delegate = wac.getBean(delegateName, HttpServlet.class);
delegate.init(new WrapServletConfig(config));
}
@Override
public void destroy()
{
delegate.destroy();
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
delegate.service(req, res);
}
public void setInitParams(Properties initParams)
{
this.initParams = initParams;
}
private class WrapServletConfig implements ServletConfig, ServletContext
{
// we override ServletContext also because it exposes getInitParameterNames()/getInitParemter()
private ServletConfig delegate;
private ServletContext delegateContext;
public WrapServletConfig(ServletConfig config)
{
this.delegate = config;
this.delegateContext = config.getServletContext();
}
@Override
public String getServletName()
{
return delegate.getServletName();
}
@Override
public ServletContext getServletContext()
{
return delegate.getServletContext();
}
@Override
public String getInitParameter(String s)
{
return initParams.getProperty(s);
}
@Override
public Enumeration getInitParameterNames()
{
return initParams.propertyNames();
}
@Override
public Object getAttribute(String s)
{
return delegateContext.getAttribute(s);
}
@Override
public Enumeration getAttributeNames()
{
return delegateContext.getAttributeNames();
}
@Override
public void setAttribute(String s, Object o)
{
delegateContext.setAttribute(s, o);
}
@Override
public void removeAttribute(String s)
{
delegateContext.removeAttribute(s);
}
@Override
public String getServletContextName()
{
return delegateContext.getServletContextName();
}
// the remainer is just straight delegation to ServletContext
@Override
public ServletContext getContext(String s)
{
return delegateContext.getContext(s);
}
@Override
public int getMajorVersion()
{
return delegateContext.getMajorVersion();
}
@Override
public int getMinorVersion()
{
return delegateContext.getMinorVersion();
}
@Override
public String getMimeType(String s)
{
return delegateContext.getMimeType(s);
}
@Override
public Set getResourcePaths(String s)
{
return delegateContext.getResourcePaths(s);
}
@Override
public URL getResource(String s)
throws MalformedURLException
{
return delegateContext.getResource(s);
}
@Override
public InputStream getResourceAsStream(String s)
{
return delegateContext.getResourceAsStream(s);
}
@Override
public RequestDispatcher getRequestDispatcher(String s)
{
return delegateContext.getRequestDispatcher(s);
}
@Override
public RequestDispatcher getNamedDispatcher(String s)
{
return delegateContext.getNamedDispatcher(s);
}
@Override
public Servlet getServlet(String s)
throws ServletException
{
return delegateContext.getServlet(s);
}
@Override
public Enumeration getServlets()
{
return delegateContext.getServlets();
}
@Override
public Enumeration getServletNames()
{
return delegateContext.getServletNames();
}
@Override
public void log(String s)
{
delegateContext.log(s);
}
@Override
public void log(Exception e, String s)
{
delegateContext.log(e, s);
}
@Override
public void log(String s, Throwable throwable)
{
delegateContext.log(s, throwable);
}
@Override
public String getRealPath(String s)
{
return delegateContext.getRealPath(s);
}
@Override
public String getServerInfo()
{
return delegateContext.getServerInfo();
}
}
}
答案 1 :(得分:2)
听起来像ServletWrapperController
就是你需要的。
Spring Controller实现 包装它的servlet实例 内部管理。这样包裹着 servlet在此之外是未知的 控制器;它的整个生命周期是 这里介绍
<bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController">
<property name="servletClass" value="org.apache.struts.action.ActionServlet"/>
<property name="servletName" value="action"/>
<property name="initParameters">
<props>
<prop key="config">/WEB-INF/struts-config.xml</prop>
</props>
</property>
</bean>
这将让您像其他Spring控制器一样处理遗留servlet,因此您可以使用普通的Spring MVC处理程序映射来路由它。