一、过滤器、监听器配置在web.xml配置文件中,拦截器则不一定,可通过 web.xml加载配置拦截器的文件:
1、在DispatcherServlet的初始化过程中,Spring会在web应用的WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml的配置文件,
生成文件中定义的bean。这些bean会覆盖在全局范围(global cope)中定义的同名的bean。如果servletName-servlet.xml不在默认路径下必须显示指定。
2、 在 XXX-servlet.xml文件中配置拦截器
<mvc:interceptors>
<!-- 多个拦截器,顺序执行 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.suning.im.server.quanzi.interceptor.AuthenInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
3、web.xml配置:注意顺序为 filter-->listener-->servlet
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<servlet-name>quanzi-api</servlet-name>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>quanzi-api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/quanzi-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>quanzi-api</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>