web.xml 中以编码方式添加filter并设置初始化参数AbstractAnnotationConfigDispatchServletInitializer
web.xml中配置filter
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<display-name></display-name>
<filter>
<filter-name>testFilter</filter-name>
<filter-class>com.bolin.core.TestFilter.java</filter-class>
<init-param>
<param-name>specialUrl</param-name>
<param-value>
/login,/register
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>testFilter</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
假如有以下场景:
testFilet的初始化参数specialUrl在开发环境,测试环境和生产环境中有所区别,那么在系统提交测试,或者上线前必须要先修改web.xml,这样的话就会比较繁琐,给系统升级或测试增加额外开销。
这样就出现一个需求,希望能在系统启动时读取不同环境的配置信息来初始化web.xml,这个配置信息可以是库中的数据,或者固定的配置文件,最好这些配置信息能够一劳永逸,兼容开发环境,测试环境,生产环境,那以后就省事了。有一个开关去控制是什么环境,那就读取相应环境的配置数据。
编码实现web.xml配置filter
那在这样的需求下,web.xml就需要以编码的方式来实现配置。spring4.0以上的版本支持web.xml的编码配置。实现AbstractAnnotationConfigDispatcherServletInitializer接口,在servlet3.0中web.xml启动时会检测该接口实现类,从能够在实现类中去配置filter。
package com.bolin.core;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Properties;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return null;
}
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
// 系统启动时注册filter
FilterRegistration testFilter = servletContext.addFilter("testFilter", TestFilter.class);
// 设置init param, param可以从properties文件中读取或其他方式获取,提供一个想法
testFilter.setInitParameter("specialUrl", "/login,/register");
testFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "*.*");
super.onStartup(servletContext);
}
@Override
protected Dynamic registerServletFilter(ServletContext arg0, Filter filter) {
return super.registerServletFilter(arg0, filter);
}
}
推荐一篇相同类型的博客:xml零配置之AbstractAnnotationConfigDispatcherServletInitializer
需要注意的是以上的实现,依赖servlet-api-3.0.jar和spring-webmvc-4.0以上版本jar包。
web.xml 中以编码方式添加filter并设置初始化参数AbstractAnnotationConfigDispatchServletInitializer的更多相关文章
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- web.xml中配置spring.xml的三种方式
我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...
- websphere中的会话超时设置 和 web应用中web.xml中session-timeout关系
Tomcat默认的会话的超时时间设置 设置Tomcat session有效期的三种方式有: 1.在tomcat/conf/web.xml中修改session-timeout的值,该设置是TOMCAT全 ...
- 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中
把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...
- [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别
原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...
- Web.xml中Filter过滤器标签几个说明
在研究liferay框架中看到Web.xml中加入了过滤器的标签,可以根据页面提交的URL地址进行过滤,发现有几个新标签没用过,下面就介绍以下几个过滤器的标签用法: <!-- 定义Filter ...
- web.xml中Filter的作用
Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...
- spring的配置文件在web.xml中加载的方式
web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...
- web.xml中Filter过滤器标签说明
原文:http://www.cnblogs.com/edwardlauxh/archive/2010/03/11/1918618.html 在研究liferay框架中看到Web.xml中加入了过滤器的 ...
随机推荐
- ENGINE_API CXSroll
#ifndef __XSROLL_H__ #define __XSROLL_H__ #include "CocoHead.h" #include "XWindow.h&q ...
- SQL业务审核与优化
审核 什么是业务审核 类似与code review 评审业务Schema和SQL设计 偏重关注性能 是业务优化的主要入口之一 审核提前发现问题,进行优化 上 ...
- Form表单——例子
Form Form的验证思路 前端:form表单 后台:创建form类,当请求到来时,先匹配,匹配出正确和错误信息. Django的Form验证实例: 创建project,进行基础配置文件配置 STA ...
- phpstrom直接运行和调试php
最近想学服务器开发,但是没找到免费的虚拟主机,好在有一大把的php主机, 于是决定学php了,但并不准备学网页制作,只是把php作为服务器逻辑处理语言. 下载xampp,打开phpstrom的设置界面 ...
- Unix系统编程()执行非局部跳转:setjmp和longjmp
使用库函数setjmp和longjmp可执行非局部跳转(local goto). 术语"非局部(nonlocal)"是指跳转目标为当前执行函数之外的某个位置. C语言里面有个&qu ...
- 小贝_redis 高级应用-事务
redis高级应用-事务 一.redis的事务 二.redis实现事务 三.redis事务问题 一.redis的事务 事务提供了一种"将多个命令打包,然后一次性.按顺序地运行"的机 ...
- 记一次redis攻击
服务器挖矿病毒的排查过程 事情起因:朋友的一台阿里云主机,登录特别卡,找我看看 这一看就感觉出问题了,机器特别卡,top看了一眼,cpu几乎是100%运行 但是奇怪的是用top命令完全看不出来哪个进程 ...
- WEB 项目中JAVA取得WEBROOT物理路径
http://wwwzhouhui.iteye.com/blog/504330 ———————————————————————————————————————————————————————————— ...
- [android] AndroidManifest.xml - 【 manifest -> application】
语法: <application android:allowTaskReparenting=["true" | "false"] android:back ...
- sours insight 使用技巧
最终在团队的气氛下还是拿回了source insight编译器: Source Insight实质上是一个支持多种开发语言(java,c ,c 等等)的编辑器,只不过由于其查找.定位.彩色显示等功能的 ...