Spring和Servlet的整合



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

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

  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")

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);

    }

}

  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;

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);

    }

}

  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>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值