Spring Web Flow Bean 作用域和 JSF

工程 | Ben Hale | 2007年5月8日 | ...

我最近完成了 Spring Web Flow 中的一个有趣问题。这个问题(SWF-163)涉及到为 Spring Web Flow 的内部作用域添加 Spring 2.0 bean 作用域支持。实现本身并不是那么有趣(毕竟 Scope 接口相当容易实现),但我想提一下你如何在你的应用程序中使用类似这样的东西。

Spring 2.0 作用域

在 Spring 1.x 中,我们有单例和原型 bean 作用域的概念,但表示法是固定的,并且用 singleton="[true | false]" 来描述并不是特别清晰。因此在 Spring 2.0 中,这种表示法从 XSD 样式的配置中移除,现在您看到的是更清晰的表示法 scope="[singleton | prototype | ...]"。Spring 本身增加了三个更多的 bean 作用域;requestsessionglobalSession,它们与 Web 应用程序相关。

通过 Spring Web Flow 1.1 的最新快照,我们现在看到了三个主要的 Web Flow 作用域的 bean 作用域:flashflowconversation


<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 提供的任何三个范围。

作用域 bean 和 JSF

目前,这些新范围最引人注目的用途是在 JSF 中。通过在标准的 Spring bean 定义文件中使用 scope 约定而不是在流定义中使用 <var/> 约定,JSF 用户体验会更接近标准的 JSF 行为。要为 JSF 添加此功能,您需要注册标准的 Spring JSF DelegatingVariableResolver。其他定义(FlowNavigationHandlerFlowPhaseListener)是使用 Spring 和 JSF 的标准配置。

<?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 属性进行范围限定。

结论

这样做的最终结果是,JSF 用户现在可以使用类似于内置样式的语法,但在一个功能更强大的容器中。

如果您想使用这个新功能,它将在 Spring Web Flow 1.1-m1 版本中很快发布,或者您可以通过下载最新的 Spring Web Flow 1.1-m1 夜间快照来获得预览。

获取 Spring 新闻通讯

通过 Spring 新闻通讯保持联系

订阅

领先一步

VMware 提供培训和认证,助您加速进步。

了解更多

获得支持

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件,只需一份简单的订阅。

了解更多

即将举行的活动

查看 Spring 社区所有即将举行的活动。

查看所有