Spring配置Servlet

在讲之前,我们先来看看spring配置文件中为Servlet注入到底能不能成功。首先,写一个随服务器启动的Servlet,Servlet中定义一个类变量,并定义set方法。随便在set方法中向控制台输出些东西,然后在doGet,doPost或service方法中调用该类对象的一个方法。配置好配置文件,启动服务器。仔细观察控制台,我们发现Servlet启动过程中已经执行了set方法,说明改servlet已经被成功注入。我们再打开浏览器访问下这个Servlet,发现服务器报了空指针异常。大体意思是说你在doGet或doPost或service方法中调用的那个变量是空。为什么会这样?其实也不难理解,我们使用spring的目的就是为了让spring为我们来提供一个已经被注入好的一个实例。而servlet是不同的,servlet是有生命周期的,而这个并不归属spring管理,而是由web容器管理的。那么当servlet刚刚创建的时候,spring可以为servlet注入,当你访问的时候,由于servlet是单实例多线程的,所以,servlet信息被重置,刚刚被注入的对象又为null了。       那么该怎么处理这个问题呢?其实也不难,只要我在获得Serlet的时候,用从Spring获得,而不是由web容器获得就可以了。要想获得Spring中管理的bean肯定要获得ApplicationContext对象,前面说过,在web开发中要获得ApplicationContext对象需要获得ServletContext,所以,需要有一个Servlet,而这个Servlet所要做的就是获得Spring中定义的那个Servlet。而Servlet最终还是要归web容器管理的,所以要归还给web容器,简单的讲就是在定义的这个Servlet中所有方法都用从Spring定义中的获得的那个Servlet去处理就可以了。所以,这个Servlet我们可以写成:public class ServletToBeanProxy extends GenericServlet { private String targetBean; private Servlet proxy; public void init() throws ServletException {   System.out.println("proxy init");   this.targetBean = getInitParameter("targetBean");   getServletBean();   proxy.init(getServletConfig()); } public void service(ServletRequest req, ServletResponse res)    throws ServletException, IOException {   proxy.service(req, res); } private void getServletBean() {   // ---------- Linstner版 ------------   // WebApplicationContext wac =   // WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());   // this.proxy = (Servlet) wac.getBean(targetBean);   //------------ Servlet版 ------------   ApplicationContext context = WebApplicationContextUtils     .getRequiredWebApplicationContext(this.getServletContext());   this.proxy = (Servlet) context.getBean(targetBean);     //通过ServletContext获得     // ApplicationContext context = (ApplicationContext) this   // .getServletContext()   // .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } }       web.xml可以做如下配置:ProxyBean    com.up72.servlet.ServletToBeanProxy   targetBeanactionServlet1ProxyBean*.do其中init-param中的targetBean就是你在Spring中配置的那个Servlet。      其他的J2EE API的注入网上都有配置方法,有兴趣的可以去查找。我就不列举了。之所以讲Servlet是因为在MVC中Servlet充当了控制器的角色,是MVC的关键和核心。在Servlet往往需要大量注入service层的一些类,然后在类中调用。虽然,配置好Listener或是Servlet以后,你可以通过ServletContext的getAttribute获得Application对象,然后调用getBean()方法获得你所需要的bean,不过,这样就写死了。违背了软件开发的可修改原则。 备注:如果你只需要Spring的注入功能,那么你只需要两个jar包就可以了。spring.jar和commons-logging.jar








Spring管理filter和servlet
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象  的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用

WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("beanName")来获得对象的引用,这实际上是使用了依赖查找来获得对象,并且在filter或者servlet代码中硬编码了应用对象的bean名字。为了能在filter或者servlet中感知spring中bean,可采用如下步骤来实现:

1-  将filter或者servlet作为bean定义在context.xml文件中,和要应用的bean定义放在一起;

2-  实现一个filter代理或者servlet代理,该代理用WebApplicationContext来获得在context.xml中定义的filter或者servlet的对象,并将任务委托给context.xml中定义的filter或者servlet

3-  在web.xml中用ContextLoaderListener来初始化spring  的context,同时在filter代理或者servlet代理的定义中用初始化参数来定义context.xml中filter或者servlet的bean名字(或者直接受用代理的名称获得相应的filter或者servlet的名称)。

4-  在web.xml中定义filter代理或者servlet代理的mapping.

利用这种方式就将filter或者servlet和业务对象的依赖关系用spring  来进行管理,并且不用在servlet中硬编码要引用的对象名字。



具体实例如下:
Filter
1.       在applicationContext.xml中定义filter

<bean id="springFilter" class="com.netqin.filter.SpringFilter">

              <property name="name">

                  <value>SpringFilter</value>

              </property>

       </bean>

说明:com.netqin.filter.SpringFilter为实现了javax.servlet.Filter接口的filter

2.       实现filter代理

实际上,filter代理不需要我们自己来实现,Spring提供了两种现成的filter代理

org.springframework.security.util.FilterToBeanProxy,

org.springframework.web.filter.DelegatingFilterProxy,两者只是在web.xml中的配置上略有不同,下面就让我们一起看看如何在web.xml中进行配置。



3.       配置web.xml

Ø         初始化spring的context

因为是使用spring来管理,所以在使用filter前先要初始化spring的context,一般来说配置如下:

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

Ø         Filter配置:

²        FilterToBeanProxy

<filter>

        <filter-name> springFilter </filter-name>

        <filter-class>

            org.springframework.security.util.FilterToBeanProxy

        </filter-class>

        <init-param>

            <param-name>targetBean</param-name>

            <param-value>springFilter</param-value>

        </init-param>

    </filter>

说明:需要为FilterToBeanProxy提供上下文参数,这里我们配置的是targetBean属性,它告诉spring在context中查找的bean名称,所以当请求被过滤器拦截后FilterToBeanProxy会在applicationContext.xml中会查找id为springFilter的bean.

我们也可以配置targetClass属性,意思就是查找该类型的bean.

²        DelegatingFilterProxy

<filter>

        <filter-name>springFilter</filter-name>

        <filter-class>

            org.springframework.web.filter.DelegatingFilterProxy

        </filter-class>

    </filter>



说明:使用DelegatingFilterProxy时不需要配置任何参数,spring会根据filter-name的名字来查找bean,所以这里spring会查找id为springFilter的bean.



4.       配置filter的mapping

<filter-mapping>

        <filter-name>springFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>



OK!filter配置完成。推荐使用DelegatingFilterProxy,应为配置上更简单。

Servlet
Servlet的配置与Filter的配置十分相似

1.       在applicationContext.xml中定义servlet

    <bean id="springServlet" class="com.netqin.servlet.SpringServlet">

              <property name="name">

                  <value>SpringServlet</value>

              </property>

       </bean>

说明:com.netqin.servlet.SpringServlet继承自

javax.servlet.http.HttpServlet

2.       实现servlet代理

与filter不同,spring没有为servlet提供代理实现,需要我们自己来创建,不过放心,创建一个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;

import org.springframework.web.context.support.WebApplicationContextUtils;



public class ServletToBeanProxy extends GenericServlet {

    private String targetBean;

    private Servlet proxy;

    public void init() throws ServletException {

        this.targetBean = getInitParameter("targetBean");

        getServletBean();

        proxy.init(getServletConfig());

    }

    public void service(ServletRequest req, ServletResponse res)

            throws ServletException, IOException {

        proxy.service(req, res);

    }

    private void getServletBean() {

        WebApplicationContext wac = WebApplicationContextUtils

                .getRequiredWebApplicationContext(getServletContext());

        this.proxy = (Servlet) wac.getBean(targetBean);

    }

}

说明:相信看了代码就明白了,它利用targetBean属性在spring中查找相应的servlet,

这很像FilterToBeanProxy的方式,所以我为其取名为ServletToBeanProxy。当然,我们也可以使用类似于DelegatingFilterProxy的方式,只需要将上述代码中标记为黄色的部分修改为this.targetBean =this.getServletName();即可,我们相应的命名为DelegatingServletProxy。

3.       配置web.xml

Ø         初始化spring的context

与filter中的说明一致,不再赘述。

Ø         Servlet配置:

²        ServletToBeanProxy

<servlet>

        <servlet-name>springServlet</servlet-name>

        <servlet-class>

            com.netqin.servlet.proxy.ServletToBeanProxy

        </servlet-class>

        <init-param>

            <param-name>targetBean</param-name>

            <param-value>springServlet</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

²        DelegatingServletProxy

<servlet>

        <servlet-name>springServlet</servlet-name>

        <servlet-class>

            com.netqin.servlet.proxy.DelegatingServletProxy

        </servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

4.       配置servlet的mapping

<filter-mapping>

        <filter-name>springServlet</filter-name>

        <url-pattern>/servlet/*</url-pattern>

    </filter-mapping>

OK!servlet的配置完成。推荐使用DelegatingServletProxy,应为配置上更简单。

猜你喜欢

转载自zylics.iteye.com/blog/1189636