领先一步
VMware 提供培训和认证,助您加速进步。
了解更多我最近完成了 Spring Web Flow 中的一个有趣问题。这个问题(SWF-163)涉及到为 Spring Web Flow 的内部作用域添加 Spring 2.0 bean 作用域支持。实现本身并不是那么有趣(毕竟 Scope 接口相当容易实现),但我想提一下你如何在你的应用程序中使用类似这样的东西。
通过 Spring Web Flow 1.1 的最新快照,我们现在看到了三个主要的 Web Flow 作用域的 bean 作用域:flash、flow 和 conversation。
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flash"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flow"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>
要利用这些 bean 范围,您需要使用新的 1.1 版本配置(包含在 Web Flow jar 中)并为您的 bean 定义添加一个单独的元素。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.1.xsd">
<flow:enable-scopes/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>
</beans>
给定应用程序上下文中只需要存在一次 <enable-scopes/> 标签,它将允许您使用 Spring Web Flow 提供的任何三个范围。
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<application>
<navigation-handler>
org.springframework.webflow.executor.jsf.FlowNavigationHandler
</navigation-handler>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
<lifecycle>
<phase-listener>
org.springframework.webflow.executor.jsf.FlowPhaseListener
</phase-listener>
</lifecycle>
</faces-config>
与之前的 Web Flow 启用配置相比,主要的改变是现在变量解析器是来自 Spring 而不是 Spring Web Flow。当 JSP 页面查找 sale 变量时,JSF 会将 bean 解析委托给 Spring,并且 bean 实例将根据其定义上的 scope 属性进行范围限定。
如果您想使用这个新功能,它将在 Spring Web Flow 1.1-m1 版本中很快发布,或者您可以通过下载最新的 Spring Web Flow 1.1-m1 夜间快照来获得预览。