Spring JavaConfig, bean's custom scopes and an

2019-03-18 16:52发布

I have a problem to solve: 1) our project is using Spring JavaConfig approach (so no xml files) 2) I need to create custom scope, example in xml looks like this:

<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>

I figured it out with JavaConfig it will looks something like this:

    @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;
}

Correct me if I'm wrong with my assumption.

3) I need to annotate my class something as @Component (scope="workflow") again xml configuration would look like this:

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

So basically the question is - am I right with my assumption to use @Component (scope="workflow") or it is expected to be in some other way?

Thanks

1条回答
闹够了就滚
2楼-- · 2019-03-18 16:56

You need to use annotation @Scope. Like this:

@Scope("workflow")

It is also possible to create custom scope qualifier:

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

and use it this way:

@Component
@WorkflowScoped 
class SomeBean
查看更多
登录 后发表回答