Web filter中如何引用Spring的bean

本文探讨了在Spring环境下,Web Filter如何引用和初始化Spring Bean的问题。文章详细分析了Filter、Listener、Servlet的加载顺序,指出Filter在Spring通过监听器启动后才初始化,导致直接注解无法注入Bean。解决方案是在Filter的init方法中通过ApplicationContext获取Bean,或者在doFilter方法中注入,但后者效率较低。此外,还介绍了Spring启动配置的流程。
摘要由CSDN通过智能技术生成

今天写了写了阵微信公众号玩,Spring+Struts搭的,有个需求是签名验证,需要在本地缓存一个用户ticket,而获取ticket的逻辑在某个spring service中实现的。

对所有的jsp页面,想要注入对应的信息,而且不配置struts的话,想了想,最好用filter。实现了filter之后,准备注入service,这个时候启动webproject发现,service为null.怎么回事?为什么spring环境中明明通过@Component注入了对应的FIlter,并且通过@Autowired注入了对应的Service,为何Web请求的时候获取不到对应的Service?

应该有这么几个问题需要我搞清楚:1.web 加载的时候有filter,listener,servlet等元素,他们是如何加载的,顺序如何,是不是filter的环境spring 管不到。2.我的配置是如何启动?3.应该如何解决这个问题,即如何在filter中使用service?

一、Filter,Listener,Servlet对比

首先这三个东东来自web.xml,那么web.xml是干嘛的?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
</web-app>

第一行给出了XML头,用来定义字符编码和xml版本,一般的xml文件都有这个。紧接着是web-app元素,这个元素的xmlns,即xml namespace是定义该元素所支持的子元素的。可以看出,在web.xml中是namespace是来自于sun的javaee页面。


可以看出上面配置的,是06年发布的2.5标准。而最新的已经是13年4月发布的3.1。我看了下web-app_3.1.xsd,其中引入了

</pre><pre name="code" class="html"><xsd:include schemaLocation="web-common_3_1.xsd"/>

这个文件中定义这些元素:

 <xsd:element name="filter"
                   type="javaee:filterType"/>
      <xsd:element name="filter-mapping"
                   type="javaee:filter-mappingType"/>
      <xsd:element name="listener"
                   type="javaee:listenerType"/>
      <xsd:element name="servlet"
                   type="javaee:servletType"/>
      <xsd:element name="servlet-mapping"
                   type="javaee:servlet-mappingType"/>
可以看到这几个元素都定义了name和type,这个type又是在哪儿定义的呢?原来web-common_3_1.xsd又引入了javaee.xsd和jsp_2_3.xsd。在javaee.xsd中,定义了这些type。

1.filterType和filterMappingTYpe的定义(在web-common_3.1.xsd中):

  <xsd:complexType name="filterType">
    <xsd:annotation>
      <xsd:documentation>

<span style="font-size:14px;"><span style="color:#3366ff;">        <strong>The filterType is used to declare a filter in the web
        application. The filter is mapped to either a servlet or a
        URL pattern in the filter-mapping element, using the
        filter-name value to reference.</strong></span><span style="color:#ff0000;"> </span></span>Filters can access the
        initialization parameters declared in the deployment
        descriptor at runtime via the FilterConfig interface.
        
        Used in: web-app
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:group ref="javaee:descriptionGroup"/>
      <xsd:element name="filter-name"
                   type="javaee:filter-nameType"/>
      <xsd:element name="filter-class"
                   type="javaee:fully-qualified-classType"
                   minOccurs="0"
                   maxOccurs="1">
        <xsd:annotation>
          <xsd:documentation>

            The fully qualified classname of the filter.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
      <xsd:element name="async-supported"
                   type="javaee:true-falseType"
                   minOccurs="0"/>
      <xsd:element name="init-param"
                   type="javaee:param-valueType"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:annotation>
          <xsd:documentation>

            The init-param element contains a name/value pair as
            an initialization param of a servlet filter
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>
<xsd:complexType name="filter-mappingType">
    <xsd:annotation>
      <xsd:documentation>

        Declaration of the filter mappings in this web
        application is done by using filter-mappingType. 
        The container uses the filter-mapping
      <span style="background-color: rgb(255, 255, 255);"><span style="color:#000099;">  <strong>declarations to decide which filters to apply to a request,
        and in what order.</strong> </span></span>The container matches the request URI to
        a Servlet in the normal way. To determine which filters to
        apply it matches filter-mapping declarations either on
        servlet-name, or on url-pattern for each filter-mapping
        element, depending on which style is used. The order in
        which filters are invoked is the order in which
        filter-mapping declarations that match a request URI for a
        servlet appear in the list of filter-mapping elements.The
        filter-name value must be the value of the filter-name
        sub-elements of one of the filter declarations in the
        deployment descriptor.
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:element name="filter-name"
                   type="javaee:filter-nameType"/>
      <xsd:choice minOccurs="1"
                  maxOccurs="unbounded">
        <xsd:element name="url-pattern"
                     type="javaee:url-patternType"/>
        <xsd:element name="servlet-name"
                     type="javaee:servlet-nameType"/>
      </xsd:choice>
      <xsd:element name="dispatcher"
                   type="javaee:dispatcherType"
                   minOccurs="0"
                   maxOccurs="5"/>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>


2.listener定义在javaee_7.xsd.
  <xsd:complexType name="listenerType">
    <xsd:annotation>
      <xsd:documentation>

        <span style="background-color: rgb(255, 255, 255);"><span style="color:#000099;"><strong>The listenerType indicates the deployment properties for a web
        application listener bean.</strong></span></span>
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:group ref="javaee:descriptionGroup"/>
      <xsd:element name="listener-class"
                   type="javaee:fully-qualified-classType">
        <xsd:annotation>
          <xsd:documentation>

            The listener-class element declares a class in the
            application must be registered as a web
            application listener bean. The value is the fully
            qualified classname of the listener class.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>

3.Servlet定义在web-commmon_3.1.xsd中,

 <xsd:complexType name="servletType">
    <xsd:annotation>
      <xsd:documentation>

        <span style="background-color: rgb(255, 255, 255);"><span
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值