- 浏览: 520070 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (88)
- Ant (4)
- Apache (2)
- chm (1)
- Javadoc api (7)
- Spring (9)
- Maven (7)
- Hibernate (4)
- Postgresql (1)
- Velocity (1)
- 有意思的题目 (3)
- Solr (4)
- eclipse (7)
- jpa (3)
- java (8)
- excel (1)
- jsp (1)
- ubuntu (2)
- 设计模式 (1)
- memcached (1)
- tomcat (4)
- Exception (2)
- 实用小技巧 (5)
- javascript (1)
- jquery (1)
- nginx (5)
- html (1)
- 一步一步升级配置 (8)
- windows (1)
- 注册表 (1)
- chrome (2)
- tiles (1)
- git (3)
- json (1)
- log (5)
- node.js (1)
- gradle (1)
- spring-session (5)
- mysql (1)
- ajax (1)
- dubbo (1)
- springmvc (1)
- loxia (1)
最新评论
-
akang_cdx:
JPEGImageEncoder encoder = JPEG ...
【飞天奔月出品】javax.imageio.IIOException: Can't create output stream!tomcat 验证码,图片(原理) -
飞天奔月:
zuxianghuang 写道解决问题了,谢谢不客气
Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议 -
zuxianghuang:
解决问题了,谢谢
Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议 -
飞天奔月:
加上了 jdk8 String.join性能不错
字符串拼接汇总(性能对比) -
fanlei77:
JDK8的String.join可以加一下一起比较
字符串拼接汇总(性能对比)
<mvc:annotation-driven>和DefaultAnnotationHandlerMapping 配置教训
- 博客分类:
- Spring
前一阵子,负责公司用户中心项目的开发,发现了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 成功取到值,牺牲了我的"历史技术积累"了
一次失败的经历,与君共勉
评论
<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>
这一段拦截不到吧?
发表评论
-
当商品编码遇到了.号, spring restful @PathVariable 应对措施
2018-07-09 17:36 16941. 背景 今天我们的某 ... -
【飞天奔月出品】一步一步升级配置13: 将tiles 升级到3.0.1 (spring集成tiles指南)
2013-07-16 01:29 3802tiles 是一个 模板框架.目的是为了简化前端用户界面的 ... -
一步一步升级配置7:开发环境使用maven tomcat plugin 来运行项目,大幅度提到开发效率
2012-08-31 17:00 4423背景: 目前官方商 ... -
一步一步升级spring配置5:使用P标签及内部bean简化spring quartz配置
2012-06-28 11:22 44901.原始做法: spring quartz配置文件,一 ... -
一步一步升级spring配置4:使用SPEL和Util标签配置spring xml参数
2012-05-26 16:06 46751.原始做法: spring xml配置文件,参数直接 ... -
使用springmvc 发布rss
2012-03-05 01:59 2018很喜欢google阅读器,一直就像写个自己的rss玩玩,碰巧这 ... -
升级到hibernate search 4.0,hibernate core4 ,spring 3.1 初步代码变化
2011-12-26 20:00 50441.hibernate search 已经有4.0.0. ... -
开箱即用(配置过程),使用spring 减少配置hibernate mapping 的痛苦(AnnotationSessionFactoryBean)
2011-11-24 18:43 16403今天在给公司新来的同事讲解项目架构的时候,项目中用到了JPA ...
相关推荐
- **显式配置拦截器**:通过`<mvc:interceptors>`标签显式地声明并配置拦截器,避免使用`<mvc:annotation-driven />`的默认设置。 - **排除默认拦截器**:如果`<mvc:annotation-driven />`包含默认拦截器,可以考虑...
在Spring MVC框架中,`mvc:annotation-driven`和`mvc:message-converters`是两个非常重要的元素,它们在处理基于注解的控制器和数据转换方面起着关键作用。本篇文章将深入探讨这两个组件的工作原理以及如何在实际...
在Spring MVC框架中,`mvc:annotation-driven`是Spring MVC配置中的一个重要元素,它使得我们的应用能够支持基于注解的控制器、数据绑定、格式化转换器和服务端验证等功能。这篇博客将深入探讨`mvc:annotation-...
9. **日期和时间格式化**:当处理日期和时间类型时,`<mvc:annotation-driven>`可以自动根据配置的区域设置进行格式化。 总之,`<mvc:annotation-driven/>`是Spring MVC中一个强大的工具,极大地简化了我们的开发...
在 Spring 框架中,事务管理器是通过 `<tx:annotation-driven>` 元素来配置的。在多数据源配置中,我们可以定义多个事务管理器,每个事务管理器对应一个数据源。例如,我们可以定义两个事务管理器 `...
除了上述方法,还可以在`<mvc:annotation-driven>`元素内部使用`<mvc:message-converters>`配置自定义的`HttpMessageConverter`,效果与上述配置相同: ```xml <mvc:annotation-driven> <mvc:message-converters> ...
在XML配置中,我们需要声明`DispatcherServlet`并配置`<mvc:annotation-driven>`以启用注解驱动: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
这个文件包含了所有在Spring MVC 4.2版本中可以使用的XML配置元素和属性,例如`<mvc:annotation-driven>`、`<bean>`、`<context:component-scan>`等。这些配置元素允许开发者声明式地配置控制器、视图解析器、转换...
- `<mvc:annotation-driven>`:启用基于注解的控制器支持,使得我们可以使用如@Controller、@RequestMapping等注解。 - `<bean>`定义处理器映射器(HandlerMapping)和处理器适配器(HandlerAdapter):它们帮助...
2. **`<mvc:annotation-driven>`**: 此元素启用Spring MVC对注解的处理,使得我们可以在控制器类的方法上使用像@RequestMapping这样的注解来处理HTTP请求。它还提供了数据绑定、转换服务、验证等特性。 3. **`<bean...
- 通过`<aop:config>`和`<tx:annotation-driven>`可以配置AOP代理和基于注解的事务管理。 总的来说,Spring MVC通过XML配置或注解方式,使得开发者能够灵活地组织和配置应用程序的各个组件,包括控制器、模型对象...
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- ...
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> `...
- `tx:advice` 和 `aop config` 配置通常是用于配置AOP(面向切面编程)的,但在使用`<tx:annotation-driven>`时,它们通常是不必要的,因为该元素本身就包含了处理事务切面的功能。所以,我们可以将这些配置注释掉...
XSD文件如`spring-mvc.xsd`定义了Spring MVC配置的元素和属性,例如`<mvc:annotation-driven>`用于启用基于注解的控制器,`<bean>`定义了一个可被Spring管理的Bean。 2. **Spring AOP**: Spring AOP模块提供了...
<mvc:annotation-driven/> <!-- 资源处理 --> <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/> <!-- 其他自定义配置... --> </beans> ``` 通过以上步骤,可以成功搭建一个基于...
赠送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;...
- `<mvc:annotation-driven>` 开启对注解的自动扫描,支持 @RequestMapping 等注解。 - `<context:component-scan>` 指定包扫描范围,找到带有 @Controller 注解的类。 例如: ```xml <beans> <context:...
- **启用注解驱动**:使用`<mvc:annotation-driven/>`来启用Spring MVC的注解功能。 - **配置视图解析器**:定义视图解析器`InternalResourceViewResolver`来处理视图名与实际视图文件之间的映射关系。 #### 三、...