春季JavaConfig,bean的定制范围和注释 [英] Spring JavaConfig, bean's custom scopes and annotations

查看:118
本文介绍了春季JavaConfig,bean的定制范围和注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决一个问题:
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(范围=工作流程)
再次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(范围=工作流程),或者预计将在一些其他的方式

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

这篇关于春季JavaConfig,bean的定制范围和注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆