13

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

user2174470
  • 131
  • 1
  • 3
  • 1
    I just got the warning `@Bean method getWorkflowScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues.` Just FYI, your method should read `@`Bean public **static** CustomScopeConfigurer – Brian Schlenker Jan 18 '16 at 08:38

2 Answers2

9

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
Michail Nikolaev
  • 3,733
  • 22
  • 18
  • To add onto this, since the original question is using AWS Simple Workflow which generates those classes using AspectJ, you would use `@Bean` and `@Scope("workflow")`/`@WorkflowScoped` in a configuration class. – mkobit Nov 28 '14 at 19:44
0

I faced a similar situation in my project, see here.

In essence, you have to pass the WorkflowScope class instance as an argument in the customScope() method and use it; otherwise, it won't work:

@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;
}
John Allison
  • 966
  • 1
  • 10
  • 30