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 社群中所有即將舉行的活動。

檢視全部