ApplicationContext是Spring的核心,是”应用的容器”,在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext;WebApplicationContext的初始化方式和BeanFactory.ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,也就是说它必须拥有Web容器的前提下才能完成启动的工作。
spring提供了用于启动WebApplicationContext的监听器:org.springframework.web.context.ContextLoaderListener,使用它可以在web应用启动的时候来初始化WebApplicationContext。如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。
在Web.xml中配置如下内容:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
如果Spring框架采用B/S系统,通过ServletContext对象获取ApplicationContext对象,然后再通过它获取需要的类实例,常用的两种方法如下:
1、WebApplicationContextwac =
WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext())。
2、WebApplicationContextwac =
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
方法1:在Servlet在init()方法中从Spring容器中获取bean
public class SpringServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WebApplicationContext wac;
StuBizImpl biz;
public void doGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
biz.show();
}
public void doPost(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
biz=(StuBizImpl)wac.getBean("stuBizImpl");
}
}
上面的方法虽然简单,但是很显然,Servlet自身没有在Spring的容器的管理之内。
方法二、编写Servlet的代理bean
-
被代理的Servlet
package cn.com.bochy.servlet;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import cn.com.bochy.biz.StuBizImpl;
@Controller(value="SpringServletProxy")
public class SpringServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WebApplicationContext wac;
@Resource
private StuBizImpl biz;
public void setBiz(StuBizImpl biz) {
this.biz = biz;
}
public void doGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
biz.show();
}
public void doPost(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
doGet(request, response);
}
}
-
代理的Servlet
package cn.com.bochy.servlet;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
public class ServletProxy extends GenericServlet {
private Servlet proxy;
private String servletName;
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
super.init();
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(this.getServletContext());
servletName = getServletName();
System.out.println(servletName);// 这里就是web.xml中的<servlet-name>中的属性值,被代理的Servlet的名字和这个属性值保持一致
proxy = (Servlet) wac.getBean(servletName);
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
proxy.service(arg0, arg1);
}
}
-
Web.xml中的配置信息
<servlet-name>SpringServletProxy</servlet-name>
<servlet-class>cn.com.bochy.servlet.ServletProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringServletProxy</servlet-name>
<url-pattern>/ServletProxy</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>