根据弹簧配置文件记录不同的流量



在我的应用程序中有不同的弹簧配置文件:开发测试,...

问题是,有没有办法根据激活的弹簧轮廓来记录流量?

例如,我有流: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="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location path=".../aFlow.xml" />
    <webflow:flow-location path=".../bFlow.xml" />
</webflow:flow-registry>

背景:如果弹簧轮廓开发被激活,bFlow.xml可能无法访问。如果激活了弹簧轮廓测试,则 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>

并根据弹簧配置文件使用不同的 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 的弹簧配置文件。您有一个流 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。

最新更新