SpringBoot初始教程之Servlet、Filter、Listener配置详解
1.介绍
通过之前的文章来看,springboot涵盖了很多配置,但是往往一些配置是采用原生的servlet进行的,但是在springboot中不需要配置web.xml的
因为有可能打包之后是一个jar包的形式,这种情况下如何解决?springboot 提供了两种方案进行解决
2.快速开始
2.1 方案一
方案一采用原生servlet3.0的注解进行配置、@webservlet 、@weblistener、@webfilter是servlet3.0 api中提供的注解
通过注解可以完全代替web.xml中的配置,下面是一个简单的配置
indexservlet
@webservlet(name = "indexservlet",urlpatterns = "/hello") public class indexservlet extends httpservlet { @override public void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { resp.getwriter().print("hello word"); resp.getwriter().flush(); resp.getwriter().close(); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { this.doget(req, resp); } }
indexlistener
@weblistener public class indexlistener implements servletcontextlistener { private log log = logfactory.getlog(indexlistener.class); @override public void contextinitialized(servletcontextevent servletcontextevent) { log.info("indexlistener contextinitialized"); } @override public void contextdestroyed(servletcontextevent servletcontextevent) { } }
indexfilter
@webfilter(urlpatterns = "/*", filtername = "indexfilter") public class indexfilter implements filter { log log = logfactory.getlog(indexfilter.class); @override public void init(filterconfig filterconfig) throws servletexception { log.info("init indexfilter"); } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { log.info("dofilter indexfilter"); filterchain.dofilter(servletrequest,servletresponse); } @override public void destroy() { } }
上面配置完了,需要配置一个核心的注解@servletcomponentscan,具体配置项如下,可以配置扫描的路径
@target(elementtype.type) @retention(retentionpolicy.runtime) @documented @import(servletcomponentscanregistrar.class) public @interface servletcomponentscan { @aliasfor("basepackages") string[] value() default {}; @aliasfor("value") string[] basepackages() default {}; class<?>[] basepackageclasses() default {}; }
把注解加到入口处启动即可
@springbootapplication @servletcomponentscan public class appapplication { public static void main(string[] args) throws exception { springapplication.run(appapplication.class, args); } }
2.2 方案二
方案二是采用自己springboot 配置bean的方式进行配置的,springboot提供了三种beanfilterregistrationbean、servletregistrationbean、servletlistenerregistrationbean 分别对应配置原生的filter、servlet、listener,下面提供的三个配置和方案一采用的方式能够达到统一的效果
@bean public servletregistrationbean indexservletregistration() { servletregistrationbean registration = new servletregistrationbean(new indexservlet()); registration.addurlmappings("/hello"); return registration; } @bean public filterregistrationbean indexfilterregistration() { filterregistrationbean registration = new filterregistrationbean(new indexfilter()); registration.addurlpatterns("/"); return registration; } @bean public servletlistenerregistrationbean servletlistenerregistrationbean(){ servletlistenerregistrationbean servletlistenerregistrationbean = new servletlistenerregistrationbean(); servletlistenerregistrationbean.setlistener(new indexlistener()); return servletlistenerregistrationbean; }
3.总结
两种方案在使用上有差别,但是在内部springboot的实现上是无差别的,即使使用的是servlet3.0注解,也是通过扫描注解
转换成这三种bean的filterregistrationbean、servletregistrationbean、servletlistenerregistrationbean
4.扩展
大家在使用的时候有没有发觉,其实springboot在使用springmvc的时候不需要配置dispatcherservlet的,因为已经自动配置了, 但是如果想要加一些初始配置参数如何解决,方案如下:
@bean public servletregistrationbean dispatcherregistration(dispatcherservlet dispatcherservlet) { servletregistrationbean registration = new servletregistrationbean(dispatcherservlet); registration.addurlmappings("*.do"); registration.addurlmappings("*.json"); return registration; }
可以通过注入dispatcherservlet 然后用servletregistrationbean包裹一层 动态的加上一些初始参数
本文代码:springboot-servlet-filter-listener_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。