`

<mvc:annotation-driven>和DefaultAnnotationHandlerMapping 配置教训

阅读更多

前一阵子,负责公司用户中心项目的开发,发现了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 会自动注册DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter两个实例,

根据断点,这个时候启动的 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 成功取到值,牺牲了我的"历史技术积累"了

 

一次失败的经历,与君共勉

 

查询资料:http://forum.springsource.org/showthread.php?81238-Conflict-between-lt-mvc-annotation-driven-gt-and-DefaultAnnotationHandlerMapping

分享到:
评论
3 楼 lijunwyf 2015-06-01  
是不是<mvc:annotation-driven />接下来的<mvc:interceptors> 还是可以正常拦截的?多谢
2 楼 lijunwyf 2015-06-01  
大牛,你的最后一段代码,<mvc:annotation-driven /> 中默认添加了DefaultAnnotationHandlerMapping,

<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>

这一段拦截不到吧?
1 楼 宋建勇 2013-09-09  
怒赞!有分析问题过程,nice!

相关推荐

    拦截器与冲突解决

    - **显式配置拦截器**:通过`&lt;mvc:interceptors&gt;`标签显式地声明并配置拦截器,避免使用`&lt;mvc:annotation-driven /&gt;`的默认设置。 - **排除默认拦截器**:如果`&lt;mvc:annotation-driven /&gt;`包含默认拦截器,可以考虑...

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    在Spring MVC框架中,`mvc:annotation-driven`和`mvc:message-converters`是两个非常重要的元素,它们在处理基于注解的控制器和数据转换方面起着关键作用。本篇文章将深入探讨这两个组件的工作原理以及如何在实际...

    SpringMVC源码总结(二)mvc:mvc:annotation-driven背后的那些事

    在Spring MVC框架中,`mvc:annotation-driven`是Spring MVC配置中的一个重要元素,它使得我们的应用能够支持基于注解的控制器、数据绑定、格式化转换器和服务端验证等功能。这篇博客将深入探讨`mvc:annotation-...

    (代码)SpringMVC第12讲:<mvc:annotation-driven/>

    9. **日期和时间格式化**:当处理日期和时间类型时,`&lt;mvc:annotation-driven&gt;`可以自动根据配置的区域设置进行格式化。 总之,`&lt;mvc:annotation-driven/&gt;`是Spring MVC中一个强大的工具,极大地简化了我们的开发...

    spring的annotation-driven配置事务管理器详解 (多数据源配置

    在 Spring 框架中,事务管理器是通过 `&lt;tx:annotation-driven&gt;` 元素来配置的。在多数据源配置中,我们可以定义多个事务管理器,每个事务管理器对应一个数据源。例如,我们可以定义两个事务管理器 `...

    SpringMVC中解决@ResponseBody注解返回中文乱码问题

    除了上述方法,还可以在`&lt;mvc:annotation-driven&gt;`元素内部使用`&lt;mvc:message-converters&gt;`配置自定义的`HttpMessageConverter`,效果与上述配置相同: ```xml &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; ...

    spring-mvc 注解方式xml配置

    在XML配置中,我们需要声明`DispatcherServlet`并配置`&lt;mvc:annotation-driven&gt;`以启用注解驱动: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    spring-mvc-4.2.xsd.zip

    这个文件包含了所有在Spring MVC 4.2版本中可以使用的XML配置元素和属性,例如`&lt;mvc:annotation-driven&gt;`、`&lt;bean&gt;`、`&lt;context:component-scan&gt;`等。这些配置元素允许开发者声明式地配置控制器、视图解析器、转换...

    Spring MVC 配置请求的默认处理器.rar

    - `&lt;mvc:annotation-driven&gt;`:启用基于注解的控制器支持,使得我们可以使用如@Controller、@RequestMapping等注解。 - `&lt;bean&gt;`定义处理器映射器(HandlerMapping)和处理器适配器(HandlerAdapter):它们帮助...

    Spring3.X @MVC - (一)重要的配置文件

    2. **`&lt;mvc:annotation-driven&gt;`**: 此元素启用Spring MVC对注解的处理,使得我们可以在控制器类的方法上使用像@RequestMapping这样的注解来处理HTTP请求。它还提供了数据绑定、转换服务、验证等特性。 3. **`&lt;bean...

    spring mvc

    - 通过`&lt;aop:config&gt;`和`&lt;tx:annotation-driven&gt;`可以配置AOP代理和基于注解的事务管理。 总的来说,Spring MVC通过XML配置或注解方式,使得开发者能够灵活地组织和配置应用程序的各个组件,包括控制器、模型对象...

    springboot 基础简易实例, maven项目

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;parent&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- ...

    spring—mvc -json

    &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt; &lt;/mvc:message-converters&gt; &lt;/mvc:annotation-driven&gt; `...

    基于框架的Web开发-基于注解的事务配置.doc

    - `tx:advice` 和 `aop config` 配置通常是用于配置AOP(面向切面编程)的,但在使用`&lt;tx:annotation-driven&gt;`时,它们通常是不必要的,因为该元素本身就包含了处理事务切面的功能。所以,我们可以将这些配置注释掉...

    spring的部分常用xsd文件,包含mvc,aop,beans,tx,task多个版本

    XSD文件如`spring-mvc.xsd`定义了Spring MVC配置的元素和属性,例如`&lt;mvc:annotation-driven&gt;`用于启用基于注解的控制器,`&lt;bean&gt;`定义了一个可被Spring管理的Bean。 2. **Spring AOP**: Spring AOP模块提供了...

    springMVC框架搭建及详解

    &lt;mvc:annotation-driven/&gt; &lt;!-- 资源处理 --&gt; &lt;mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/&gt; &lt;!-- 其他自定义配置... --&gt; &lt;/beans&gt; ``` 通过以上步骤,可以成功搭建一个基于...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    springMVC.doc

    - `&lt;mvc:annotation-driven&gt;` 开启对注解的自动扫描,支持 @RequestMapping 等注解。 - `&lt;context:component-scan&gt;` 指定包扫描范围,找到带有 @Controller 注解的类。 例如: ```xml &lt;beans&gt; &lt;context:...

    SpringMVC入门

    - **启用注解驱动**:使用`&lt;mvc:annotation-driven/&gt;`来启用Spring MVC的注解功能。 - **配置视图解析器**:定义视图解析器`InternalResourceViewResolver`来处理视图名与实际视图文件之间的映射关系。 #### 三、...

Global site tag (gtag.js) - Google Analytics