[英]Spring JavaConfig, bean's custom scopes and annotations
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: 我有一个要解决的问题: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>
I figured it out with JavaConfig it will looks something like this: 我用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;
}
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: 3)我需要以@Component(scope =“ workflow”)的形式对我的类进行注释,xml配置再次如下所示:
<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? 因此,基本上的问题是-我是否可以使用@Component(scope =“ workflow”),还是应该以其他方式使用?
Thanks 谢谢
You need to use annotation @Scope
. 您需要使用注释
@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
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; 本质上,您必须将
WorkflowScope
类实例作为参数传递给customScope()
方法,然后使用它。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.