How to set name of qualifier bean at runtime in Spring

We can set the names of qualifiers for the bean (for example @Qualifier(value="myBeanQualifiedName")

). but I want to know how to set the classifier name at runtime in the class @Configuration

.

Suppose, based on the application logic, I want to specify the bean name as a qualifier in the config file.
Editors:

ConcreteBean is a child of 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;
        }

    }
}   

      

Suppose the getMySpecifiedBean () method is being called from different places, and for each location, delta instances are needed, but of type ConcretBean (). Instances are different from each other because the setService () method sets different property values. Hence b1 and b2 will make a difference to the service instance where they are used.

In my example above, depending on the condition, the QUALIFIER_NAME will be changed. Then, Can we assign the prepared QUALIFIER_NAME a qualifier name to the newly created bean? and how to get such beans with the qualifier name (the classifier name is known)? For example, elsewhere, the
String qalifierName = "QulifierName" + is prepared by the ConditionedString; @Autowired @Qualified (qalifierName)

String qalifierName2 = "QulifierName" + createdConditionedString2;
@Autowired @Qualified (qalifierName2)

Also, you might be thinking what if we hardcode the qualifiers ... but think what if 20 or more instances are created there? We must repeat the codes.

+3


source to share


1 answer


Expanding on my "Look at the ServiceLocatorFactoryBean comment":

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;
    }
}

      

InterfaceIServiceFactory

This will allow you to map the beans prototype to the decision string (the main function of your question).

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

      

InterfaceIBean

This will allow you to handle the returned bean (prototype) from the factory.



public interface IBean
{
    //TODO
}

      

Interface IDecisionMaker

.
This will make your decision processes independent of your factory code. The implementation takes your string condition

and returns the property name which will result in an IBean from the IServiceFactory implementation / configuration.

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

      

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>

      

+3


source







All Articles