简体   繁体   English

如何在Spring中的运行时设置bean限定符名称

[英]How to set a bean qualifier name at run time in Spring

We can set qualifier names for a bean (for example @Qualifier(value="myBeanQualifiedName") ) . 我们可以为bean设置限定符名称(例如@Qualifier(value="myBeanQualifiedName") )。 but I want to know how to set a Qualifier name at run time in a @Configuration class. 但我想知道如何在运行时在@Configuration类中设置限定符名称。

Assume based on a application logic I want to give a name to a bean as a qualifier in a configuration file. 假设基于应用程序逻辑,我想给bean命名为配置文件中的限定符。
EDITED: 编辑:

ConcreteBean is a child class of MyAbstractBean. ConcreteBean是MyAbstractBean的子类。

@Configuration
public class MyBeanFactory {

    @Bean
    public MyAbstractBean getMySpecifiedBean(String condition){
        String QUALIFIER_NAME="QulifierName"+condition;
        if(//some condition here ){
        //How to set a qualifier name :QUALIFIER_NAME for this ConcreteBean instance?    
       MyAbstractBean b1= new ConcreteBean();
       b1.setService(new AnotherService1);  // and set some field values to this concrete bean
       return b1;
        }
        else  {
         MyAbstractBean b2= new ConcreteBean();
       b2.setService(new AnotherService2);  
       return b2;
        }

    }
}   

Assume getMySpecifiedBean() method is called from different locations and each location need difference instances, but the type of ConcretBean(). 假设从不同位置调用getMySpecifiedBean()方法,并且每个位置都需要差异实例,但需要ConcretBean()的类型。 Instances are differ each other because of the setService() method set different property values. 由于setService()方法设置了不同的属性值,因此实例彼此不同。 Therefore, the b1 and b2 will do difference things with its service instance where they are used. 因此,b1和b2将与使用它们的服务实例做不同的事情。

In my above example, based on the condition, the QUALIFIER_NAME name will be changed.Then Can we assign the prepared QUALIFIER_NAME to the qualifier name to the newly created bean? 在上面的示例中,根据条件,QUALIFIER_NAME名称将被更改。那么我们是否可以将准备好的QUALIFIER_NAME分配给新创建的bean的限定符名称? and how to get such beans with the qualifier name (qualifier name is known) ? 以及如何使用限定符名称获取此类bean(限定符名称已知)? For example in another location, 例如,在另一个地方,
String qalifierName="QulifierName" + preparedConditionedString; String qalifierName =“QulifierName”+ preparedConditionedString; @Autowired @Qualified (qalifierName) @Autowired @Qualified(qalifierName)

String qalifierName2="QulifierName" + preparedConditionedString2; String qalifierName2 =“QulifierName”+ preparedConditionedString2;
@Autowired @Qualified (qalifierName2) @Autowired @Qualified(qalifierName2)

Also you may think what if we hard code the qualifiers.. but think what if there 20 or more instances to be created ? 您也可以考虑如果我们对限定符进行硬编码会怎么样?但是如果要创建20个或更多实例,请考虑一下? We have to repeat the codes. 我们必须重复这些代码。

Expanding on my "Have a look at ServiceLocatorFactoryBean" comment: 扩展我的“看看ServiceLocatorFactoryBean”评论:

public class MyBeanFactory
{
    private IServiceFactory serviceFactory;
    private IDecisionMaker decisionMaker;

    public IBean createNewInstance(final String condition)
    {
      String conditionResult = decisionMaker.decide(condition);
      return serviceFactory.getNewInstance(conditionResult);
    }

    public void setServiceFactory(final IServiceFactory serviceFactory)
    {
        this.serviceFactory = serviceFactory;
    }
    public void setDecisionMaker(final IDecisionMaker decisionMaker)
    {
        this.decisionMaker = decisionMaker;
    }
}

The IServiceFactory Interface This will allow you to map prototype beans to a decision string (core function of you question). IServiceFactory接口这将允许您将原型bean映射到决策字符串(您的问题的核心功能)。

public interface IServiceFactory
{
    IBean getNewInstance(String identifier);
}

The IBean Interface This will allow you to handle the returned bean (prototype) from the factory. IBean接口这将允许您从工厂处理返回的bean(原型)。

public interface IBean
{
    //TODO
}

The IDecisionMaker Interface. IDecisionMaker接口。 This will make your decision making processes independed of you factory code. 这将使您的决策制定流程不受工厂代码的限制。 An implementation takes your condition string and returns a property name, that will result in a IBean from the IServiceFactory implementation/configuration. 实现接受condition字符串并返回属性名称,该属性名称将从IServiceFactory实现/配置中生成IBean。

public interface IDecisionMaker
{
    String decide(String condition);
}

The Spring xml Konfiguration Spring xml Konfiguration

<! implementations of the IBean interface -->
<bean id="myBeanRed" class="..." scope="prototype" />
<bean id="myBeanBlue"  class="..." scope="prototype" />
<bean id="myBeanGreen" class="..." scope="prototype" />

<!-- the decision maker -->
<bean id="decisionMaker" class="...">
       <!-- define your decision making here  like:
            condition(color=red)->red
            condition(color=blue)->blue
            condition(ELSE)->green
        -->
</bean>

<!-- the abstract factory -->
<bean id="myBeanServiceFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
    <property name="serviceLocatorInterface" value="...IServiceFactory "/>
    <property name="serviceMappings">
        <props>
            <prop key="red">myBeanRed</prop>
            <prop key="blue">myBeanBlue</prop>
            <prop key="green">myBeanGreen</prop>
        </props>
    </property>
</bean>

<!-- the factory -->
<bean id="myBeanFac" class="...MyBeanFactory" scope="singleton">
    <property name="serviceFactory" ref="myBeanServiceFactory" />
    <property name="decisionMaker" ref="decisionMaker" />
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM