我在我的应用程序中有两个servlet,我希望将一个A类对象注入到两个servlet中,我也希望在整个应用程序中使用相同的ApplicationContext,即在SO的这个问题的第一个答案中提到的两个servlet: Spring注入Servlet
现在我经历了很多像这样的问题,但找不到与我的问题相符的东西.为了更好地解释,我在这里写一个粗略的代码
public class servletOne extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } public class servletTwo extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
所以上面是applicationContext.xml中的两个服务器我想将一个对象传递给这两个servlet,因此按照惯例,我想要一个像这样的功能:
我不知道这是否可能,我是春天的新手,我只有依赖注入的基本知识.
如果有人能帮我这个,我真的很感激.这将清除我的许多疑虑并帮助我在学习Spring的过程中前进.
这是web.xml
contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener dispatcher org.springframework.web.servlet.DispatcherServlet 2 servletOne mypackage.servletOne servletTwo mypackage.servletTwo dispatcher *.htm servletOne /servletOne servletTwo /servletTwo 300
Sotirios Del.. 15
你混淆了两个概念:Servlets和Spring ApplicationContext
.Servlet由Servlet容器管理,让我们以Tomcat为例.在ApplicationContext
由Spring管理.
Servlet
在部署描述符中声明为a 时
servletOne mypackage.servletOne servletOne /servletOne
Servlet容器将创建您的mypackage.servletOne
类的实例,注册它,并使用它来处理请求.这就是它对DispatcherServlet
Spring MVC的基础所做的.
Spring是一个ApplicationContext
用于管理多个bean 的IoC容器.该ContextLoaderListener
负载根ApplicationContext
(无论从任何位置,你告诉它).在DispatcherServlet
使用了根上下文,也必须加载自己的.上下文必须具有适当的配置DispatcherServlet
才能工作.
在Spring上下文中声明一个bean,比如
无论它与
web.xml中声明的类型相同,都是完全不相关的.上面的bean与
web.xml中的声明无关.
正如我在这里的回答一样,因为ContextLoaderListener
它将ApplicationContext
它创建ServletContext
的属性作为一个属性,ApplicationContext
可供任何Servlet容器管理对象使用.因此,您可以覆盖HttpServlet#init(ServletConfig)
自定义HttpServlet
类,如此
@Override public void init(ServletConfig config) throws ServletException { super.init(config); ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); this.someObject = (SomeBean)ac.getBean("someBeanRef"); }
假设你的root ApplicationContext
包含一个名为的bean someBeanRef
.
还有其他替代方案.例如,这.
你混淆了两个概念:Servlets和Spring ApplicationContext
.Servlet由Servlet容器管理,让我们以Tomcat为例.在ApplicationContext
由Spring管理.
Servlet
在部署描述符中声明为a 时
<servlet> <servlet-name>servletOne</servlet-name> <servlet-class>mypackage.servletOne</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletOne</servlet-name> <url-pattern>/servletOne</url-pattern> </servlet-mapping>
Servlet容器将创建您的mypackage.servletOne
类的实例,注册它,并使用它来处理请求.这就是它对DispatcherServlet
Spring MVC的基础所做的.
Spring是一个ApplicationContext
用于管理多个bean 的IoC容器.该ContextLoaderListener
负载根ApplicationContext
(无论从任何位置,你告诉它).在DispatcherServlet
使用了根上下文,也必须加载自己的.上下文必须具有适当的配置DispatcherServlet
才能工作.
在Spring上下文中声明一个bean,比如
<bean id="servletFirst" class="mypackage.servletOne"> <property name="message" ref="classObject" /> </bean>
无论它与<servlet>
web.xml中声明的类型相同,都是完全不相关的.上面的bean与<servlet>
web.xml中的声明无关.
正如我在这里的回答一样,因为ContextLoaderListener
它将ApplicationContext
它创建ServletContext
的属性作为一个属性,ApplicationContext
可供任何Servlet容器管理对象使用.因此,您可以覆盖HttpServlet#init(ServletConfig)
自定义HttpServlet
类,如此
@Override public void init(ServletConfig config) throws ServletException { super.init(config); ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); this.someObject = (SomeBean)ac.getBean("someBeanRef"); }
假设你的root ApplicationContext
包含一个名为的bean someBeanRef
.
还有其他替代方案.例如,这.