根据 Spring 配置文件注册不同的流

Register different flows depending on Spring profile

在我的应用程序中有不同的 spring 配置文件:developmenttest、...

问题是,有没有办法根据激活的 spring 配置文件注册流?

例如我有以下流程:aFlow.xml、bFlow.xml。

如果 spring 配置文件开发已激活,我想要类似

<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location path=".../aFlow.xml" />
</webflow:flow-registry>

如果 spring 配置文件测试已激活,那么我想要类似

的东西
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location path=".../aFlow.xml" />
    <webflow:flow-location path=".../bFlow.xml" />
</webflow:flow-registry>

背景:如果spring配置文件开发被激活,bFlow.xml可能无法访问。如果 spring 配置文件测试被激活,aFlow.xml 和 bFlow.xml 应该可以访问。

目前我有以下解决方案。我定义

<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location path=".../aFlow.xml" />
</webflow:flow-registry>

<webflow:flow-registry id="flowRegistryTest" flow-builder-services="flowBuilderServices" parent="flowRegistry">
    <webflow:flow-location path=".../bFlow.xml" />
</webflow:flow-registry>

并根据 spring 配置文件使用不同的 FlowHandlerMapping:

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry"/>
    <property name="defaultHandler">
        <!-- If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named
            "intro" -->
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
    </property>
</bean>

<beans profile="test">
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping" primary="true">
        <property name="flowRegistry" ref="flowRegistryTest"/>
        <property name="defaultHandler">
            <!-- If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named
            "intro" -->
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
        </property>
    </bean>
</beans>

提前致谢!

我找到了两个解决我所述问题的方法。

假设以下初始情况:您有一个名为 test 的 spring 配置文件。您有流量 aFlow.xml 和流量 bFlow.xml。无论哪个配置文件处于活动状态,您都想注册 aFlow,并且仅当配置文件测试处于活动状态时才想注册 bFlow。

第一个解决方案使用基于 xml 的流注册表配置:

<beans profile="!test">
    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path=".../aFlow.xml"/>
    </webflow:flow-registry>
</beans>
<beans profile="test">
    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path=".../aFlow.xml"/>
        <webflow:flow-location path=".../bFlow.xml"/>
    </webflow:flow-registry>
</beans>

第二种解决方案使用基于 spring java 的流注册表配置:

@Configuration
public class FlowRegistryConfiguration extends AbstractFlowConfiguration {

    @Bean(name = "flowRegistry")
    public FlowDefinitionRegistry flowDefinitionRegistry() {

        FlowDefinitionRegistryBuilder builder = getFlowDefinitionRegistryBuilder(
            (FlowBuilderServices) getApplicationContext().getBean("flowBuilderServices"))
            .addFlowLocation(".../aFlow.xml");

        List<String> activeProfiles = Arrays.asList(getApplicationContext().getEnvironment().getActiveProfiles());

        if (activeProfiles.contains("test")) {
            builder = builder.addFlowLocation(".../bFlow.xml");
        }

        return builder.build();
    }
}

我认为第二种解决方案更好,因为您不会在两个不同的地方使用相同的 ID。