13

我有一个问题要解决:1)我们的项目使用 Spring JavaConfig 方法(所以没有 xml 文件)2)我需要创建自定义范围,xml 中的示例如下所示:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
    <map>
        <entry key="workflow">
            <bean
                class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />
        </entry>
    </map>
</property>

我用 JavaConfig 弄明白了,它看起来像这样:

    @Bean
public CustomScopeConfigurer customScope () {
    CustomScopeConfigurer configurer = new CustomScopeConfigurer ();
    Map<String, Object> workflowScope = new HashMap<String, Object>();
    workflowScope.put("workflow", new WorkflowScope ());
    configurer.setScopes(workflowScope);

    return configurer;
}

如果我的假设有误,请纠正我。

3) 我需要再次将我的类注释为@Component (scope="workflow") xml 配置如下所示:

<bean id="activitiesClient" class="aws.flow.sample.MyActivitiesClientImpl" scope="workflow"/>

所以基本上问题是 - 我的假设是正确的使用 @Component (scope="workflow") 还是预期以其他方式?

谢谢

4

2 回答 2

9

您需要使用注释@Scope。像这样:

@Scope("workflow")

也可以创建自定义范围限定符:

@Qualifier
@Scope("workflow")
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface WorkflowScoped {
}

并以这种方式使用它:

@Component
@WorkflowScoped 
class SomeBean
于 2013-03-15T15:21:11.130 回答
0

我在我的项目中遇到了类似的情况,请参见此处

本质上,您必须将WorkflowScope类实例作为customScope()方法中的参数传递并使用它;否则,它将不起作用:

@Bean
public CustomScopeConfigurer customScope(WorkflowScope workflowScope) {
    CustomScopeConfigurer configurer = new CustomScopeConfigurer();
    Map<String, Object> workflowScope = new HashMap<>();
    workflowScope.put("workflow", workflowScope);
    configurer.setScopes(workflowScope);

    return configurer;
}
于 2019-10-16T13:30:18.887 回答