Nic*_*Div 6 java spring servlets
我在我的应用程序中有两个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 {
}
}
Run Code Online (Sandbox Code Playgroud)
所以上面是applicationContext.xml中的两个服务器我想将一个对象传递给这两个servlet,因此按照惯例,我想要一个像这样的功能:
<bean id="servletFirst" class="mypackage.servletOne">
<property name="message" ref="classObject" />
</bean>
<bean id="servletFirst" class="mypackage.servletTwo">
<property name="message" ref="classObject" />
</bean>
<bean id="classObject" class="mypackage.classA">
</bean>
Run Code Online (Sandbox Code Playgroud)
我不知道这是否可能,我是春天的新手,我只有依赖注入的基本知识.
如果有人能帮我这个,我真的很感激.这将清除我的许多疑虑并帮助我在学习Spring的过程中前进.
这是web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>servletOne</servlet-name>
<servlet-class>mypackage.servletOne</servlet-class>
</servlet>
<servlet>
<servlet-name>servletTwo</servlet-name>
<servlet-class>mypackage.servletTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletOne</servlet-name>
<url-pattern>/servletOne</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletTwo</servlet-name>
<url-pattern>/servletTwo</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
300
</session-timeout>
</session-config>
</web-app>
Run Code Online (Sandbox Code Playgroud)
Sot*_*lis 15
你混淆了两个概念: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>
Run Code Online (Sandbox Code Playgroud)
Servlet容器将创建您的mypackage.servletOne类的实例,注册它,并使用它来处理请求.这就是它对DispatcherServletSpring MVC的基础所做的.
Spring是一个ApplicationContext用于管理多个bean 的IoC容器.该ContextLoaderListener负载根ApplicationContext(无论从任何位置,你告诉它).在DispatcherServlet使用了根上下文,也必须加载自己的.上下文必须具有适当的配置DispatcherServlet才能工作.
在Spring上下文中声明一个bean,比如
<bean id="servletFirst" class="mypackage.servletOne">
<property name="message" ref="classObject" />
</bean>
Run Code Online (Sandbox Code Playgroud)
无论它与<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");
}
Run Code Online (Sandbox Code Playgroud)
假设你的root ApplicationContext包含一个名为的bean someBeanRef.
还有其他替代方案.例如,这.
| 归档时间: |
|
| 查看次数: |
8973 次 |
| 最近记录: |