Spring Web Flow Bean Scopes 和 JSF

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

我最近完成了 Spring Web Flow 中一個有趣的問題。這個問題 (SWF-163) 處理的是為 Spring Web Flow 的內部 scopes 增加 Spring 2.0 bean scoping 支援。 實作本身並不是那麼有趣 (畢竟 Scope 介面非常容易實作),但我想要說明如何在您的應用程式中使用類似的東西。

Spring 2.0 Scoping

在 Spring 1.x 中,我們有 singleton 和 prototype bean scopes 的概念,但表示法是固定的,並且使用 singleton="[true | false]" 並沒有特別的描述性。 所以在 Spring 2.0 中,這種表示法從 XSD 樣式的配置中移除,現在您看到一個更清楚的表示法,使用 scope="[singleton | prototype | ...]"。 Spring 本身增加了三個更多的 bean scopes;requestsessionglobalSession,它們與 Web 應用程式相關。

使用最新的 Spring Web Flow 1.1 快照,我們現在看到三個主要的 Web Flow scopes 的 bean scopes,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 scopes,您需要利用 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 貢獻的任何三個 scopes。

Scoped Beans 和 JSF

目前,這些新 scopes 最引人注目的用途是在 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 的配置的主要變更在於,現在 variable resolver 是來自 Spring 而不是 Spring Web Flow。 當 JSP 頁面尋找 sale 變數時,JSF 將委派給 Spring 進行 bean 解析,並且 bean 實例將根據其定義上的 scope 屬性進行 scoped。

結論

最終結果是,現在 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 社群中所有即將舉辦的活動。

查看全部