简体   繁体   English

Spring在运行时决定bean

[英]Spring decide bean during runtime

I have multiple provider classes ( Provider1 and Provider2 ), how do I decide what bean I use depending on the input parameter in the Processor class? 我有多个提供程序类( Provider1Provider2 ),如何根据Processor类中的输入参数决定使用哪个bean?

public class Processor{
    private Provider provider;

    public void process(String  providerName) throws Exception {
        // What should I do here to invoke either provider1 or provider2 depending on the providerName?
        provider.doOperation();
    }
}

public class Provider1  {
    public void doOperation(Exchange exchange) throws Exception {
        //Code
    }
}

public class Provider2  {
    public void doOperation(Exchange exchange) throws Exception {
        //Code
    }
}

What about somthing like this? 这样的事呢?

1# into your processor class : 1#进入您的处理器类:

public class Processor{

    private Map<Provider> providers;

    public void process(String providerName) throws Exception {
        Provider provider = providers.get(providerName);
        provider.doOperation();
    }
}

2# in your spring config: 在Spring配置中使用2#:

<bean id="provider1" class="xx.yy.zz.Provider1"/>
<bean id="provider2" class="xx.yy.zz.Provider2"/>

<bean id="processor" class="xx.yy.zz.Processor">

  <property name="providers">
    <map>
        <entry key="provider1" value-ref="provider1" />
        <entry key="provider2" value-ref="provider2" />
    </map>
  </property>

</bean>

now for example if you call processor.process("provider1") it will call provider1.doOperation() 现在,例如,如果您调用processor.process("provider1") ,它将调用provider1.doOperation()

This is the case of Factory pattern . Factory pattern就是这种情况。 You can create a ( ProviderFactory ) class, register all the providers and get provider based on value, eg: 您可以创建一个( ProviderFactory )类,注册所有提供者并根据值获取提供者,例如:

class ProviderFactory(){

    private List<Provider> providers = new ArrayList<>();

    public Provider getProvider(String input){

        if(input.equals("test1")){
            //Find based on criteria
            return provider1;
        }else if(input.equals("test2")){
            //Find based on criteria
            return provider2;
        }
    }

    public void registerProvider(Provider provider){
        providers.add(provider);
    }
}

You can call registerProvider method on application startup and add as many providers as you want. 您可以在应用程序启动时调用registerProvider方法,并根据需要添加任意数量的提供程序。 Once that is initialised, you can call getProvider method and return appropriate instance based on some criteria. 初始化后,您可以调用getProvider方法并根据某些条件返回适当的实例。

Please note that providers doesn't necessarily need to be a list , it can be any data structure. 请注意, providers并不一定要是list ,它可以是任何数据结构。 It depends on which structure suits your criteria the best. 这取决于哪种结构最适合您的标准。

Here 's documentation/more examples for Factory pattern. 是文档/更多有关Factory模式的示例。

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

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