精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-11-21
前一阵子,负责公司用户中心项目的开发,发现了spring新版本有很好用的新的命名空间 <mvc:>,可以简化<bean>的配置
而且,自从spring 3.0.4之后,对于 restful的支持更加完美
so,尝试着,在 mvc的配置文件中,使用mvc的命名空间
原先,我们是这么配置拦截器的
<bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor"/> <bean class="com.jumbo.interceptor.MetaInfoInterceptor"/> </list> </property> </bean>
看到新命名空间mvc:interceptors简洁,我们进行修改, 改造的过程中又想保留原来的配置interceptors的习惯,以留作历史技术积累习惯
产生了我这样的配置
<mvc:annotation-driven /> <!-- *************openSessionInViewInterceptor *************--> <bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- **************interceptors ************************** --> <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor" /> </list> </property> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**/*.htm" /> <bean id="metaInfoInterceptor" class="com.jumbo.web.interceptor.MetaInfoInterceptor"/> </mvc:interceptor> </mvc:interceptors>
测试了下 metaInfoInterceptor,很完美,程序很正常
杯具的事情来了
今天,我在controller 里面写了这样的代码
... MemberRank memberRank = memberRankManager.findMemberRank_byTotalConsumption(memberId); model.addAttribute("rankCode", memberRank.getRank().getCode()); ...
死活给我出现 could not initialize proxy - no Session
我就纳闷儿了,心想,哥OpenSessionInView自从 spring+hibernate 就一直用,都好几年了,写过这样的代码没有一千也有八百,怎么回事?
没办法,只有去 openSessionInViewInterceptor 加个断点,看看我的代码是否走过openSessionInViewInterceptor, 一路debug走下去,没有经过openSessionInViewInterceptor
奇怪了,那么去 DefaultAnnotationHandlerMapping 加个断点,看看初始化的时候,我的openSessionInViewInterceptor 到底设置了没有, 嘿,不看不知道,一看~真奇妙
开始的时候, interceptors 一直是null,突然又有值了,里面有我们的需要的小小的openSessionInViewInterceptor
一看log
22:06:45,511 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/api.htm] onto handler 'apiController' 22:06:45,697 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/aboutus.htm] onto handler 'helpCenterController' 22:06:45,697 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/contactus.htm] onto handler 'helpCenterController' 22:06:45,698 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/disclaimer.htm] onto handler 'helpCenterController' 22:06:45,698 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/privacy.htm] onto handler 'helpCenterController' 22:06:45,698 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/sitemap.htm] onto handler 'helpCenterController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/errors/errors.htm] onto handler 'mainController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/index.htm] onto handler 'mainController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/bulletin/show.htm] onto handler 'mainController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/errors/404.htm] onto handler 'mainController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/admin/refreshmeta.htm] onto handler 'mainController' 22:06:45,866 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/memguide/showguide.htm] onto handler 'mainController' 22:06:46,049 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/memberpoint/show.htm] onto handler 'memberPointController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/makedefaultaddress.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberaddressdelete.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberview.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberupdate.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressList.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressadd.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressView.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressUpdate.htm] onto handler 'memberController' 22:06:46,210 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberrankview.htm] onto handler 'memberController' 22:06:46,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/qq.htm] onto handler 'oauthController' 22:06:46,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/qqcallback.htm] onto handler 'oauthController' 22:06:46,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/taobao.htm] onto handler 'oauthController' 22:06:46,370 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/taobaocallback.htm] onto handler 'oauthController' 22:06:46,537 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/myorderinfo.htm] onto handler 'orderDetailController' 22:06:46,537 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/myorderdetail.htm] onto handler 'orderDetailController' 22:06:46,537 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/synchronizationview.htm] onto handler 'orderDetailController' 22:06:46,537 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/synchronizationorder.htm] onto handler 'orderDetailController' 22:06:46,697 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/validateEmail.json] onto handler 'mailSubscriptionController' 22:06:52,269 INFO [SimpleUrlHandlerMapping] Mapped URL path [/images/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 22:06:52,270 INFO [SimpleUrlHandlerMapping] Mapped URL path [/css/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1' 22:06:52,271 INFO [SimpleUrlHandlerMapping] Mapped URL path [/scripts/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2' 22:06:52,937 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/api.htm] onto handler 'apiController' 22:06:53,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/aboutus.htm] onto handler 'helpCenterController' 22:06:53,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/contactus.htm] onto handler 'helpCenterController' 22:06:53,369 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/disclaimer.htm] onto handler 'helpCenterController' 22:06:53,370 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/privacy.htm] onto handler 'helpCenterController' 22:06:53,370 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/helpcenter/sitemap.htm] onto handler 'helpCenterController' 22:06:53,561 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/errors/errors.htm] onto handler 'mainController' 22:06:53,561 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/index.htm] onto handler 'mainController' 22:06:53,561 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/bulletin/show.htm] onto handler 'mainController' 22:06:53,561 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/errors/404.htm] onto handler 'mainController' 22:06:53,561 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/admin/refreshmeta.htm] onto handler 'mainController' 22:06:53,562 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/memguide/showguide.htm] onto handler 'mainController' 22:06:53,737 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/memberpoint/show.htm] onto handler 'memberPointController' 22:06:53,913 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/makedefaultaddress.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberaddressdelete.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberview.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberupdate.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressList.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressadd.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressView.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/addressUpdate.htm] onto handler 'memberController' 22:06:53,914 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/member/memberrankview.htm] onto handler 'memberController' 22:06:54,097 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/qq.htm] onto handler 'oauthController' 22:06:54,097 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/qqcallback.htm] onto handler 'oauthController' 22:06:54,097 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/taobao.htm] onto handler 'oauthController' 22:06:54,097 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/oauth/taobaocallback.htm] onto handler 'oauthController' 22:06:54,273 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/myorderinfo.htm] onto handler 'orderDetailController' 22:06:54,273 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/myorderdetail.htm] onto handler 'orderDetailController' 22:06:54,273 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/synchronizationview.htm] onto handler 'orderDetailController' 22:06:54,273 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/order/synchronizationorder.htm] onto handler 'orderDetailController' 22:06:54,457 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/validateEmail.json] onto handler 'mailSubscriptionController' 22:07:00,366 INFO [AbstractTilesApplicationContextFactory] Initializing Tiles2 application context. . . 22:07:00,366 INFO [AbstractTilesApplicationContextFactory] Finished initializing Tiles2 application context. 22:07:00,632 INFO [TilesAccess] Publishing TilesContext for context: org.springframework.web.servlet.view.tiles2.SpringTilesApplicationContextFactory$SpringWildcardServletTilesApplicationContext 22:07:00,648 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/views.xml] 22:07:00,676 INFO [GenericWebApplicationContext] Refreshing org.springframework.web.context.support.GenericWebApplicationContext@13bef50: startup date [Mon Nov 21 22:07:00 CST 2011]; parent: WebApplicationContext for namespace 'spring-servlet' 22:07:00,688 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9cf542: defining beans [json]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1654 22:07:00,737 INFO [DispatcherServlet] FrameworkServlet 'spring': initialization completed in 19213 ms
DefaultAnnotationHandlerMapping 被启动了两遍,controller 被Mapped了两遍
mvc:annotation-driven 会自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例, 根据断点,这个时候启动的 DefaultAnnotationHandlerMapping,我的 interceptors 没有起作用,仅在我再次配置的 DefaultAnnotationHandlerMapping才起了作用
而我controller 的代码,恰恰是使用了 第一次启动的 DefaultAnnotationHandlerMapping ,导致没有openSessionInViewInterceptor,产生了no session的 异常
将配置改成如下
<mvc:annotation-driven /> <mvc:interceptors> <!--*************openSessionInViewInterceptor ************* --> <bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <mvc:interceptor> <mvc:mapping path="/**/*.htm" /> <bean id="metaInfoInterceptor" class="com.jumbo.web.interceptor.MetaInfoInterceptor"/> </mvc:interceptor> </mvc:interceptors>
这下完美了, openSessionInViewInterceptor 配置成功,code 成功取到值,牺牲了我的"历史技术积累"了
一次失败的经历,与君共勉
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-11-21
好文章,刚好我们领导让我搭建spring mvc,这下学习了
|
|
返回顶楼 | |
浏览 11457 次