首页 > 其他分享 >如何使用Spring管理Filter和Servlet

如何使用Spring管理Filter和Servlet

时间:2023-06-02 17:03:25浏览次数:36  
标签:xml filter web Spring Filter servlet context spring 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中硬编码要引用的对象名字。具体实例如下:
1、spring与web配置
1.1 在applicationContext.xml中定义filter
<bean id="springFilter" class="com.sirui.filter.SpringFilter">
<property name="name">
<value>SpringFilter</value>
</property>
</bean>
说明: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,一般来说配置如下:
<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>
2、Filter配置:
2.1 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.
2.2 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.
2.3 配置filter的mapping
<filter-mapping>
<filter-name>springFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
filter配置完成。推荐使用DelegatingFilterProxy,应为配置上更简单。


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

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

 <property name="name"> 

 <value>SpringServlet</value> 

 </property> 

 </bean>


说明: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
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.sirui.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>
3.4DelegatingServletProxy
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.sirui.servlet.proxy.DelegatingServletProxy
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3.5 配置servlet的mapping
<filter-mapping>
<filter-name>springServlet</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
servlet的配置完成。推荐使用DelegatingServletProxy,应为配置上更简单。

如何使用Spring管理Filter和Servlet_xml


标签:xml,filter,web,Spring,Filter,servlet,context,spring,Servlet
From: https://blog.51cto.com/u_13538361/6404155

相关文章

  • spring为什么注入接口而不是实现类?
    首先,一般使用接口是很常用并且有益的变成技术。其次,在spring中,你可以在运行过程中注入各种实现。一个很经典的情况就是在测试阶段,注入模拟的实现类。===1.网上说jdk动态代理基于实现接口。直接注入实现类会使aop失效。没有cglib可能真的就失效了。2.解耦。假如有一天实现类的名......
  • 工厂模式配置servlet(servlet升级版)
    1、创建一个类点击查看代码packagecom.bh.controller;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;impo......
  • springboot项目rabbitmq消费者消费json格式的String,出现无限循环抛出No method found
    转:springboot项目rabbitmq消费者消费json格式的String,出现无限循环抛出Nomethodfoundforclass[B     ......
  • SpringMVC大文件分片上传/多线程上传
    ​ javaweb上传文件上传文件的jsp中的部分上传文件同样可以使用form表单向后端发请求,也可以使用ajax向后端发请求    1.通过form表单向后端发送请求         <formid="postForm"action="${pageContext.request.contextPath}/UploadServlet"method="post"e......
  • linux sh脚本启动springboot
    1、restart.sh#!/bin/bashAPP_NAME=xxxxx.jar#定义JAVA程序名LOG_FILE="$APP_NAME.log"#定义日志文件名称#查询进程并终止PID=`ps-ef|grep$APP_NAME|grep-vgrep|awk'{print$2}'`kill-9$PIDecho"$APP_NAME的进程$PID已经终止"#启动jar包,指......
  • Spring事务在哪些情况下失效
    阅读文本大概需要3分钟。0x01:如果数据库不支持事务,则失效   因为事务是作用于数据库。例如使用MySQL且引擎是MyISAM,则事务会不起作用,因为MyISAM引擎本身不支持事务;如果改成InnoDB,则可以。0x02:Service类没有被Spring管理    因为Spring的事务是基于AOP,所以如果Service......
  • Spring Boot实现xml传参和返回值
    阅读文本大概需要3分钟。    虽然json作为数据传输的格式大型其道,但是使用xml格式传输的系统还是在一些存量的系统中存在。另外WebService本身就是使用xml格式进行数据传输。今天用个小例子看看SpringBoot如何实现xml传参和返回值。1、新建maven项目,添加依赖1.<projectxm......
  • spring cloud gateway 自定义GatewayFilterFactory
    官网地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#writing-custom-route-predicate-factories参考地址:https://blog.csdn.net/myli92/article/details/127328893importcom.ruoyi.common.core.utils.StringUtils;importorg.springfr......
  • springboot gradle dockerfle
    本地打包FROMopenjdk:8-jdk-alpineRUNset-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositoriesRUNmkdir/appCOPYbuild/libs/dataExChangePlatform-0.0.1-SNAPSHOT.jar/app/dataExChangePlatform-0.0.1-SNAPSHOT.......
  • SpringBoot Vue3 Element Plus 打造分布式存储系统
    SpringBoot+Vue3+ElementPlus打造分布式存储系统download:3w51xuebccom配置IDEA热部署-devtools开发过程中频繁修改代码,每次都需要重新编译,部署,重启服务器,这无疑极大浪费了我们的时间。解决这个问题的方法就是使用热部署技术。本篇文章将介绍如何在IDEA中使用devtools实现热部署......