如何在 web.xml 中将 HttpServlet 与 Spring 应用程序上下文连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1866953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect HttpServlet with Spring Application Context in web.xml?
提问by Martin Dürrmeier
I'm trying to connect my FooServlet which extends HttpServlet with the ApplicationContext which is in the same Project. The Application Context is already used by a Wicket Servlet
我正在尝试将扩展 HttpServlet 的 FooServlet 与同一项目中的 ApplicationContext 连接起来。应用程序上下文已被 Wicket Servlet 使用
It works with
它与
servletContext = this.getServletContext();
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
(IMyBean)wac().getBean("myServiceBean")
Now I try to aviod to use explicitly Spring Classes in my Code (WebApplicationContextUtils) as it's not the IoC way.
现在我尝试避免在我的代码 (WebApplicationContextUtils) 中显式使用 Spring 类,因为它不是 IoC 方式。
The Wicket Servlet is connected with the Application context in the web.xml
Wicket Servlet 与 web.xml 中的 Application 上下文连接
<servlet>
<servlet-name>ExampleApplication</servlet-name>
<servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I found the class Spring HttpServletBean but I don't know if it serves for my Case
我找到了 Spring HttpServletBean 类,但我不知道它是否适用于我的案例
回答by Martin Dürrmeier
I found a way to inject Beans in my HttpServlet (Note:I don't need a Presentation View, otherwise there are more advanced Spring Classes)
我找到了一种在我的 HttpServlet 中注入 Beans 的方法(注意:我不需要 Presentation View,否则会有更高级的 Spring Classes)
Add a ContextLoaderListener to web.xml so that Spring's root WebApplicationContext is loaded
在 web.xml 中添加一个 ContextLoaderListener 以便加载 Spring 的根 WebApplicationContext
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Configure Servlet using Springs HttpRequestHandlerServletClass
使用 Springs HttpRequestHandlerServlet类配置 Servlet
<servlet>
<servlet-name>FooServlet</servlet-name>
<display-name>Foo Servlet</display-name>
<servlet-class>
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
</servlet>
Let your Servlet implement the org.springframework.web.HttpRequestHandlerInterface
让你的 Servlet 实现org.springframework.web.HttpRequestHandler接口
Define your Servlet as a Bean in ApplicationContext (beanID must be same as "servlet-name"). Now it's possible to inject all necassary Beans in the Spring DependencyInjection way without dependency lookup.
将您的 Servlet 定义为 ApplicationContext 中的 Bean(beanID 必须与“servlet-name”相同)。现在可以以 Spring DependencyInjection 的方式注入所有需要的 Bean,而无需进行依赖查找。
回答by Shreeni
I think you should use Spring utilities like RequestContextUtils.getWebApplicationContext(request, application); to hookup the Spring Context within your Servlet. Agreed this is no DI/IoC, but the servlet is no bean as well !
我认为您应该使用 Spring 实用程序,例如 RequestContextUtils.getWebApplicationContext(request, application); 在 Servlet 中连接 Spring Context。同意这不是 DI/IoC,但 servlet 也不是 bean!

