前言
其实这篇文章算不上是springboot的东西,我们在spring普通项目中也是可以直接使用的
设置过滤器:
以前在普通项目中我们要在web.xml中进行filter的配置,但是只从servlet 3.0后,我们就可以在直接在项目中进行filter的设置,因为她提供了一个注解@webfilter(在javax.servlet.annotation包下),使用这个注解我们就可以进行filter的设置了,同时也解决了我们使用springboot项目没有web.xml的尴尬,使用方法如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@webfilter (urlpatterns= "/*" ,filtername= "corsfilter" , asyncsupported = true ) public class corsfilter implements filter{ @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain chain) throws ioexception, servletexception { httpservletresponse response = (httpservletresponse)servletresponse; httpservletrequest request = (httpservletrequest)servletrequest; chain.dofilter(servletrequest, servletresponse); } @override public void destroy() { } } |
其实在webfilter注解中有一些属性我们需要进行设置, 比如value、urlpatterns,这两个属性其实都是一样的作用,都是为了设置拦截路径,asyncsupported这个属性是设置配置的filter是否支持异步响应,默认是不支持的,如果我们的项目需要进行请求的异步响应,请求经过了filter,那么这个filter的asyncsupported属性必须设置为true不然请求的时候会报异常。
设置拦截器:
编写一个配置类,继承org.springframework.web.servlet.config.annotation.webmvcconfigureradapter或者org.springframework.web.servlet.config.annotation.webmvcconfigurationsupport并重写addinterceptors(interceptorregistry registry)方法,其实父类的addinterceptors(interceptorregistry registry)方法就是个空方法。使用方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@configuration public class mvcconfig extends webmvcconfigurationsupport { @override public void addinterceptors(interceptorregistry registry) { interceptorregistration registration = registry.addinterceptor( new handlerinterceptor() { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { return true ; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { } }); // 配置拦截路径 registration.addpathpatterns( "/**" ); // 配置不进行拦截的路径 registration.excludepathpatterns( "/static/**" ); } } |
配置监听器:
一般我们常用的就是request级别的javax.servlet.servletrequestlistener和session级别的javax.servlet.http.httpsessionlistener,下面以servletrequestlistener为例,编写一个类实现servletrequestlistener接口并实现requestinitialized(servletrequestevent event)方法和requestdestroyed(servletrequestevent event)方法,在实现类上加上@weblistener(javax.servlet.annotation包下),如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@weblistener public class requestlistener implements servletrequestlistener { @override public void requestdestroyed(servletrequestevent sre) { system.out.println( "请求结束" ); } @override public void requestinitialized(servletrequestevent sre) { system.out.println( "请求开始" ); } } |
这样每一个请求都会被监听到,在请求处理前equestinitialized(servletrequestevent event)方法,在请求结束后调用requestdestroyed(servletrequestevent event)方法,其实在spring中有一个非常好的例子,就是org.springframework.web.context.request.requestcontextlistener类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class requestcontextlistener implements servletrequestlistener { private static final string request_attributes_attribute = requestcontextlistener. class .getname() + ".request_attributes" ; @override public void requestinitialized(servletrequestevent requestevent) { if (!(requestevent.getservletrequest() instanceof httpservletrequest)) { throw new illegalargumentexception( "request is not an httpservletrequest: " + requestevent.getservletrequest()); } httpservletrequest request = (httpservletrequest) requestevent.getservletrequest(); servletrequestattributes attributes = new servletrequestattributes(request); request.setattribute(request_attributes_attribute, attributes); localecontextholder.setlocale(request.getlocale()); requestcontextholder.setrequestattributes(attributes); } @override public void requestdestroyed(servletrequestevent requestevent) { servletrequestattributes attributes = null ; object reqattr = requestevent.getservletrequest().getattribute(request_attributes_attribute); if (reqattr instanceof servletrequestattributes) { attributes = (servletrequestattributes) reqattr; } requestattributes threadattributes = requestcontextholder.getrequestattributes(); if (threadattributes != null ) { // we're assumably within the original request thread... localecontextholder.resetlocalecontext(); requestcontextholder.resetrequestattributes(); if (attributes == null && threadattributes instanceof servletrequestattributes) { attributes = (servletrequestattributes) threadattributes; } } if (attributes != null ) { attributes.requestcompleted(); } } } |
在这个类中,spring将每一个请求开始前都将请求进行了一次封装并设置了一个threadlocal,这样我们在请求处理的任何地方都可以通过这个threadlocal获取到请求对象,好处当然是有的啦,比如我们在service层需要用到request的时候,可以不需要调用者传request对象给我们,我们可以通过一个工具类就可以获取,岂不美哉。
扩充:在springboot的启动类中我们可以添加一些applicationlistener监听器,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@springbootapplication public class demoapplication { public static void main(string[] args) { springapplication application = new springapplication(demoapplication. class ); application.addlisteners( new applicationlistener<applicationevent>() { @override public void onapplicationevent(applicationevent event) { system.err.println(event.tostring()); } }); application.run(args); } } |
applicationevent是一个抽象类,她的子类有很多比如servletrequesthandledevent(发生请求事件的时候触发)、applicationstartedevent(应用开始前触发,做一些启动准备工作)、contextrefreshedevent(容器初始化结束后触发),其他还有很多,这里不再多说,但是这些applicationlistener只能在springboot项目以main方法启动的时候才会生效,也就是说项目要打jar包时才适用,如果打war包,放在tomcat等web容器中是没有效果的。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。
原文链接:http://www.cnblogs.com/zzw-blog/p/10656181.html