在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为
request、session和global session,它们的含义和用法请大家参考Spring官方手册的相关章节。如果你想让你的容器里的某个bean拥有其中某种新的web作用域,除了在bean级上配置相应的scope属性
,还必须在容器级做一个额外的初始化配置。即在web应用的XML声明文件
web.xml中增加下述
ContextListener:
1 <web-app>
2
3 <listener>
4 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
5 </listener>
6
7 </web-app>
如果你用的是早期版本的web容器(Servlet 2.4以前),那么你要使用一个javax.servlet.Filter的实现。
1 <web-app>
2 ..
3 <filter>
4 <filter-name>requestContextFilter</filter-name>
5 <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
6 </filter>
7 <filter-mapping>
8 <filter-name>requestContextFilter</filter-name>
9 <url-pattern>/*</url-pattern>
10 </filter-mapping>
11
12 </web-app> 两种方式完成完全一样的功能:基于LocalThread将HTTP request对象绑定到为该请求提供服务的线程上。这使得具有request和session作用域的bean能够在后面的调用链中被访问到。
我一直使用的是Filter的配置方式,虽然使用的web容器Resin3.0.19是支持servlet2.4的但也懒得改为更为简洁的Listener了。使用一切正常,昨日无聊翻阅Spring的手册无意中又看到这一节,想想自己也太懒了,几句话就改了为什么不更简单呢?于是就把配置改成了Listener的方式,启动一切正常,但当请求第一个链接时居然报错了!
1 java.lang.NullPointerException
2 at org.springframework.web.context.request.RequestContextListener.requestDestroyed(RequestContextListener.java:67)
3 at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:120)
4 at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
5 at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:277)
6 at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:106)
7 at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
8 at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
9 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
10 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
11 at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
12 at javax.servlet.http.HttpServlet.service(HttpServlet.java:115)
13 at javax.servlet.http.HttpServlet.service(HttpServlet.java:92)
14 at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:106)
15 at org.extremecomponents.table.filter.AbstractExportFilter.doFilter(AbstractExportFilter.java:49)
16 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
17 at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:173)
18 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
19 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
20 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
21 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
22 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
23 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
24 at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
25 at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
26 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
27 at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
28 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
29 at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
30 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
31 at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
32 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
33 at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:81)
34 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
35 at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:217)
36 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
37 at org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
38 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
39 at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:229)
40 at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
41 at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
42 at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
43 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
44 at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178)
45 at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
46 at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
47 at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)
48 at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)
49 at com.caucho.util.ThreadPool.run(ThreadPool.java:433)
50 at java.lang.Thread.run(Thread.java:595)
51 [10:01:26.109] java.lang.NullPointerException
52 [10:01:26.109] at org.springframework.web.context.request.RequestContextListener.requestDestroyed(RequestContextListener.java:67)
53 [10:01:26.109] at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:188)
54 [10:01:26.109] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
55 [10:01:26.109] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
56 [10:01:26.109] at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)
57 [10:01:26.109] at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)
58 [10:01:26.109] at com.caucho.util.ThreadPool.run(ThreadPool.java:433)
59 [10:01:26.109] at java.lang.Thread.run(Thread.java:595)
顺便说一下,用Listenter方式配置初始化ApplicationContext的ContextLoaderListener是正常的,它也是需要servlet2.4支持的,所以应该不是因为容器支持的servlet版本问题。
换回Filter后一切正常,看了一下Spring源码也没看除个所以然,在网上找了很久也没找到答案,很是郁闷!希望路过的高人们能够不吝赐教,感激涕零!
posted on 2007-02-08 10:10
cresposhi 阅读(5520)
评论(20) 编辑 收藏