Spring和Servlet的整合

来源:互联网 发布:剑三新建角色捏脸数据 编辑:程序博客网 时间:2024/05/22 07:47


ApplicationContextSpring的核心,是应用的容器”,Web应用中,我们会用到WebApplicationContextWebApplicationContext继承自ApplicationContextWebApplicationContext的初始化方式和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对象,然后再通过它获取需要的类实例,常用的两种方法如下:

1WebApplicationContextwac =  

        WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext())

2WebApplicationContextwac =

                      WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

方法1:Servletinit()方法中从Spring容器中获取bean

publicclass SpringServletextends HttpServlet {

   privatestaticfinallongserialVersionUID = 1L;

   private WebApplicationContextwac;

   StuBizImpl biz;

   publicvoid doGet(HttpServletRequest request, HttpServletResponseresponse)

          throws ServletException, IOException {

        biz.show();

   }

   publicvoid doPost(HttpServletRequest request, HttpServletResponseresponse)

          throws ServletException, IOException {

              doGet(request, response);

   }

   publicvoid init()throws ServletException { 

          wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

           biz=(StuBizImpl)wac.getBean("stuBizImpl");

   }

}

上面的方法虽然简单,但是很显然,Servlet自身没有在Spring的容器的管理之内。

方法二、编写Servlet的代理bean

  1. 被代理的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")

publicclass SpringServletextends HttpServlet {

   privatestaticfinallongserialVersionUID = 1L;

   privateWebApplicationContext wac;

   @Resource

   private StuBizImplbiz;

   publicvoid setBiz(StuBizImpl biz) {

      this.biz = biz;

   }

   publicvoid doGet(HttpServletRequest request, HttpServletResponseresponse)

          throws ServletException, IOException {

        biz.show();

   }

   publicvoid doPost(HttpServletRequest request, HttpServletResponseresponse)

          throws ServletException, IOException {

              doGet(request, response);

   }

}

  1. 代理的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;

publicclass ServletProxyextends GenericServlet {

   private Servletproxy;

   private StringservletName;

   privatestaticfinallongserialVersionUID = 1L;

   @Override

   publicvoid 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

   publicvoid service(ServletRequest arg0, ServletResponse arg1)

          throws ServletException, IOException {

      proxy.service(arg0, arg1);

   }

}

  1. 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>

 

原创粉丝点击