在Spring Portlet MVC中,我们可以用PortletModeHandlerMapping来将Portlet模式绑定到对应的Handler上,并且再这里可以声明拦截器,示例代码如下:

  1. <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
  2. <property name="portletModeMap">
  3. <map>
  4. <entry key="view">
  5. <bean class="xx.xx.xx.envprovisioning.handlers.EnvProvisionFlowHandler" />
  6. </entry>
  7. </map>
  8. </property>
  9. <property name="interceptors">
  10. <list>
  11. <ref bean="envprovisionhandlerinterceptor"/>
  12. </list>
  13. </property>
  14. </bean>

我们结合框架代码来具体看下这个类是如何工作的。

从最基本的看,它是事先了spring mvc portlet的HandlerMapping 接口,这个接口主要定义了getHandler()方法:

  1. public interface HandlerMapping {
  2. HandlerExecutionChain getHandler(PortletRequest request) throws Exception;
  3. }

这个接口的getHandler()方法可以返回对于某种PortletRequest(RenderRequest,ActionRequest,ResourceRequest)的对应的处理方法,并且返回一个HandlerExecutionChain对象,我们可以根据自己的需要注册多个Handler(对应不同类型的PortletRequest)

而在PortletModeHandlerMapping的父类AbstractHandlerMapping中给出了getHandler()的默认实现:

  1. public final HandlerExecutionChain getHandler(PortletRequest request) throws Exception {
  2. Object handler = getHandlerInternal(request);
  3. if (handler == null) {
  4. handler = getDefaultHandler();
  5. }
  6. if (handler == null) {
  7. return null;
  8. }
  9. // Bean name or resolved handler?
  10. if (handler instanceof String) {
  11. String handlerName = (String) handler;
  12. handler = getApplicationContext().getBean(handlerName);
  13. }
  14. return getHandlerExecutionChain(handler, request);
  15. }

从这里可以看出它会先调用getHandlerInternal(request), 这个方法是定义在AbstractMapBaseHandlerMapping类中,它用来从handlerMap中提取key(比如"view")等,然后,获取Value(Handler的类名) 。

然后在第10-14行,通过这个Handler的名字,来在Spring Application中建立起对应这个handler对应的bean,并且将控制权返回给执行Handler的执行链。

而我们研究的PortletModelHandlerMapping类则主要用于从xml文件中读取参数,设置拦截器等等。在PortletModeHandlerMapping对应的bean,(也就是我们定义在xml文件的Spring Application Context上下文定义的portletModeHandlerMapping)初始化时候,它会调用PortletModeHandlerMapping的initApplicationContext()方法,如下:

  1. public void initApplicationContext() throws BeansException {
  2. super.initApplicationContext();
  3. registerHandlersByMode(this.portletModeMap);
  4. }

它回去调用父类的initApplicationContext()然后调用registerHandlerByMode(),我们分别看:

对于initApplicationContext(),它会初始化拦截器的设定,也就是对应我们一开始的xml定义中的interceptors属性,它会分别初始化并且使用这些拦截器:

  1. protected void initApplicationContext() throws BeansException {
  2. extendInterceptors(this.interceptors);
  3. initInterceptors();
  4. }

而对于registerHandlerByMode(this.portletModeMap),则会先读取xml bean定义文件中的portletModeMap属性,然后对于其中定义的每一对<map></map>元素,来根据其中的PortletMode来注册/实例化对应的Handler ,这个我们已经在上文中讨论过了。

现在我们对这个过程已经一目了然了

转载于:https://blog.51cto.com/supercharles888/1159603

关于Spring 中的PortletModeHandlerMapping相关推荐

  1. Spring中配置DataSource数据源的几种选择

    Spring中配置DataSource数据源的几种选择 在Spring框架中有如下3种获得DataSource对象的方法: 从JNDI获得DataSource. 从第三方的连接池获得DataSourc ...

  2. 详解设计模式在Spring中的应用

    设计模式作为工作学习中的枕边书,却时常处于勤说不用的尴尬境地,也不是我们时常忘记,只是一直没有记忆. 今天,螃蟹在IT学习者网站就设计模式的内在价值做一番探讨,并以spring为例进行讲解,只有领略了 ...

  3. Spring中利用applicationContext.xml文件实例化对象和调用方法

    Spring中实例化对象和调用方法入门 1.jar包和xml的准备 已上传至百度云盘,链接: https://pan.baidu.com/s/1CY0xQq3GLK06iX7tVLnp3Q 提取码: ...

  4. 【spring 5】AOP:spring中对于AOP的的实现

    在前两篇博客中,介绍了AOP实现的基础:静态代理和动态代理,这篇博客介绍spring中AOP的实现. 一.采用Annotation方式 首先引入jar包:aspectjrt.jar && ...

  5. Spring中WebApplicationContext

    ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用"容器"来表述它更容易理解一 些,ApplicationContext则是& ...

  6. Spring中使用Schedule调度

    在spring中两种办法使用调度,以下使用是在spring4.0中. 一.基于application配置文件,配置入下: 1 <bean id="jobDetail" cla ...

  7. Spring中SmartLifecycle和Lifecycle的作用和区别

    欢迎关注方志朋的博客,回复"666"获面试宝典 本文基于SpringBoot 2.5.0-M2讲解Spring中Lifecycle和SmartLifecycle的作用和区别,以及如 ...

  8. 惊呆了,Spring中竟然有12种定义bean的方法

    前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...

  9. MyBatis 和 Spring 中的 23 种设计模式,真香,拿去用吧!

    学习源码框架,为什么要从Mybatis框架入手?在 MyBatis 3.5.1 这个版中有 968 个类. 2770 个字段. 8422个方法. 42504 行代码. 186428个指令码.其源码体量 ...

最新文章

  1. DS博客作业07--查找
  2. python编辑时怎样换行_python怎么换行输入
  3. std::string中的find_first_of()和find_last_of()函数
  4. CSS3 制作魔方 - 相关立体样式
  5. Mac美化,GeekTool获取Yahoo天气图片
  6. CDN选择合适的回源策略很重要
  7. Science | 华盛顿大学Baker实验室提出新方法设计全新蛋白质
  8. 浏览器(Microsoft Edge)主页被360劫持的终极解决办法
  9. 【训练1】储蓄卡与信用卡
  10. 狂神说HTML5笔记
  11. 计算机与昆虫关系的论文,浅谈昆虫与人类的关系.doc
  12. iPhone 手机修复屏幕划痕
  13. 让Google搜索结果居中显示
  14. Python线程池与进程池
  15. vbs介绍与常见整人代码
  16. sanic教程-Sanic 应用
  17. 【华为oj】计票统计
  18. 40 道基础Dubbo 面试题及答案
  19. 手机上的android版本下载视频播放器,ZZPlayer手机版下载
  20. 互联网十种经典的创业模式

热门文章

  1. MFC串口通信上位机(采用静态库编译生成的)不能在其他电脑运行的问题
  2. 【星球知识卡片】图像风格化与翻译都有哪些核心技术,如何对其长期深入学习...
  3. 【杂谈】为什么邀请大家加入硬核知识星球有三AI
  4. python各种模块的使用
  5. 训练指南 UVALive - 3713 (2-SAT)
  6. React项目开发中的数据管理
  7. @JsonIgnoreProperties转换实体时忽略json中不存在的字段
  8. js比较查看两个数组之间是否相等
  9. 计算机网络那些事~(一)
  10. wp7 HubTile