我有一个我无法修改的第三方servlet.它接受我想要外化的init-param(来自web.xml).
我可以使用DelegatingFilterProxy外化servlet过滤器init-param.这有效地将servlet过滤器定义移动到Spring中,其中有更强大的外化工具(例如:PropertyPlaceholderConfigurer,环境变量等)
我怎样才能为servlet做这个?
最佳答案看起来你需要一个DelegatingServletProxy类,虽然这在Spring中不存在但是,我认为编码非常简单,使用DelegatingFilterProxy作为起点.servlet只实现了一些具体的方法,因此委托应该是直截了当的.
好吧,我把钱放在嘴边!
编辑:下面是DelegatingServletProxy的基本实现.
你这样使用它:
>在web.xml中设置常用的spring上下文配置/ ContextListener,为webapp设置应用程序范围的spring上下文.
>将servlet添加到web.xml,其实现类为DelegatingServletProxy.这将取代您想要更改init-params的现有serlvet.为这个新的servlet设置init参数:proxyServletClass是servlet类的名称. proxyServletParams是spring配置中的PropertIEs bean的名称.此属性bean用于为代理的servlet设置init参数.
>在spring配置中,添加一个定义init-params的新Properites bean
一些示例,在spring应用程序上下文中
示例web.xml代码段
这是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(); } }}
总结 以上是内存溢出为你收集整理的java – 如何外化web.xml servlet init-param? Servlets的Spring DelegatingFilterProxy?全部内容,希望文章能够帮你解决java – 如何外化web.xml servlet init-param? Servlets的Spring DelegatingFilterProxy?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)