资讯专栏INFORMATION COLUMN

如何使用Spring管理Filter和Servlet

amuqiao / 943人阅读

摘要:利用这种方式就将或者和业务对象的依赖关系用来进行管理,并且不用在中硬编码要引用的对象名字。配置的的配置完成。推荐使用,应为配置上更简单。

在使用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中硬编码要引用的对象名字。具体实例如下:

1、spring与web配置
1.1 在applicationContext.xml中定义filter

    
            
              SpringFilter  
            
   
   说明:com.sirui.filter.SpringFilter为实现了javax.servlet.Filter接口的filter
    实现filter代理 实际上,filter代理不需要我们自己来实现,Spring提供了两种现成的filter代理 org.springframework.security.util.FilterToBeanProxy, org.springframework.web.filter.DelegatingFilterProxy,两者只是在web.xml中的配置上略有不同,下面就让我们一起看看如何在web.xml中进行配置。

1.2. 配置web.xml

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

contextConfigLocation  
   
         /WEB-INF/applicationContext.xml  
   
   
   
   
      org.springframework.web.context.ContextLoaderListener  
   


2、Filter配置:
2.1 FilterToBeanProxy

     springFilter   
    org.springframework.security.util.FilterToBeanProxy  
      
      
        targetBean  
        springFilter  
      

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

2.2 DelegatingFilterProxy

    springFilter  
      
        org.springframework.web.filter.DelegatingFilterProxy  
      


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

    springFilter  
    /*  


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

3、Servlet 配置
Servlet的配置与Filter的配置十分相似
3.1 在applicationContext.xml中定义servlet

    
            
              SpringServlet  
           
   

说明:com.sirui.servlet.SpringServlet继承自 javax.servlet.http.HttpServlet
3.2 实现servlet代理,与filter不同,spring没有为servlet提供代理实现,需要我们自己来创建,不过放心,创建一个servlet代理十分简单,一个具体的实现如下:
package com.sirui.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。

    配置web.xml和初始化spring的context 与filter中的说明一致,不再赘述。         

3.3 ServletToBeanProxy

    springServlet  
      
        com.sirui.servlet.proxy.ServletToBeanProxy  
      
      
        targetBean  
        springServlet  
      
    1  


3.4DelegatingServletProxy

    springServlet  
      
        com.sirui.servlet.proxy.DelegatingServletProxy  
      
    1  

3.5 配置servlet的mapping

    springServlet  
    /servlet/*  

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

更多技术分享尽在java乐园

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70754.html

相关文章

  • 【图片抓取】003-JAVA WEB(上)

    摘要:图片抓取上本项目主要讲述项目的搭建和启动过程,为以后继续图片抓取的业务展示做基础。用于处理请求和响应的拦截处理。这样相比容器直接发到处理,大大减少了代码重复工作而且方便统一管理。上下文关系从上图可以看出主要在和两部分做工作。 【图片抓取】003-JAVA WEB(上) 本项目主要讲述java web项目的搭建和启动过程,为以后继续图片抓取的业务展示做基础。项目中采用tomcat+spr...

    jiekechoo 评论0 收藏0
  • Spring之旅第十站:MVC配置、上传文件、异常处理、跨重定向请求、为控制器添加通知

    摘要:依赖于对请求的支持。使用解析兼容的没有构造器参数,也没有要设置的参数,这样,在应用上下文中,将其声明为就会非常简单。默认是没有限制的整个请求的容量。 Spring MVC 高级的技术 本章内容: Spring MVC配置的替代方案 处理文件上传 在控制器中处理异常 使用flash属性 稍等还没结束 说明 如果你有幸能看到。后面的章节暂时不更新了,改变学习方式了。重要理解思想,这本书...

    leanote 评论0 收藏0
  • 面试题:SpringMVCStruts2的区别

    摘要:的入口是,而是这里要指出,和是不同的。以前认为是的一种特殊,这就导致了二者的机制不同,这里就牵涉到和的区别了。开发效率和性能高于。的实现机制有以自己的机制,用的是独立的方式。 1、Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上SpringMVC...

    isaced 评论0 收藏0
  • 详谈 Filter 过滤器

    摘要:元素用于指定过滤器的完整的限定类名。除此之外,过滤器不会被调用。参数用于访问后续过滤器。还可以为指定目标资源为某个,例如当用户访问时,会执行名字为的,这时会执行过滤器。防止中文乱码过滤器项目使用框架时。 文章首发在CSDN博客,转载请务必注明以下所有链接,否则考虑法律追究责任。 CSDN地址:http://blog.csdn.net/tzs_1041218129/article/det...

    wind5o 评论0 收藏0

发表评论

0条评论

amuqiao

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<