问题描述
我有一个问题要解决:1)我们的项目使用的是 Spring JavaConfig 方法(所以没有 xml 文件)2)我需要创建自定义范围,xml中的示例如下所示:
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>
我用 JavaConfig 发现它看起来像这样:
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) 我需要将我的类注释为 @Component (scope="workflow")再次 xml 配置将如下所示:
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"/>
所以基本上问题是 - 我假设使用 @Component (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?
谢谢
推荐答案
需要使用注解@Scope
.像这样:
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 {
}
并以这种方式使用它:
@Component
@WorkflowScoped
class SomeBean
这篇关于Spring JavaConfig,bean 的自定义作用域和注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!