Spring Web Flow Bean 作用域和 JSF

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

我最近完成了 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 符号,而不是在 flow 定义中使用 <var/> 符号, JSF 用户的体验更接近于标准 JSF 行为。 要将此功能添加到 JSF,请注册标准 Spring JSF DelegatingVariableResolver。 其他定义 (FlowNavigationHandlerFlowPhaseListener) 是将 JSF 与 Spring 一起使用的标准定义。

<?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 将委托给 Spring 进行 bean 解析,并且 bean 实例将根据其定义上的 scope 属性进行作用域限定。

结论

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

如果您想使用此新功能,它将很快随 Spring Web Flow 1.1-m1 版本一起发布,或者您可以下载最新的 Spring Web Flow 1.1-m1 nightly snapshot 来获得预览。

获取 Spring 新闻资讯

与 Spring 新闻资讯保持联系

订阅

更进一步

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

了解更多

获得支持

Tanzu Spring 在一个简单的订阅中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件。

了解更多

即将举行的活动

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

查看全部