简体   繁体   English

获取特定类型的Bean列表

[英]Get a List of Beans for a certain type

I have a certain type of class that can provide data and a registry where the different data providers are registered. 我有一类可以提供数据的类,还有一个用于注册不同数据提供者的注册表。 I am looking for an elegant way to implement this in spring boot. 我正在寻找一种在Spring Boot中实现此目标的优雅方法。 My current implementation looks like this (code is shortened for brevity): 我当前的实现如下所示(为简洁起见,代码已缩短):

public interface DataProvider{
    Data getSomeData();
}

public class Registry{
    public register(DataProvider provider){
       //add to internal list
    };

    public List<DataProvider> getProviders(){
       //return providers
    }

    public Data someAggregatedOperation(){ ... }
}

public class Provider1 implements DataProvider { ... }

public class Provider2 implements DataProvider { ... }

Now for the wiring part, and this is the part I want to change to something more elegant: 现在是布线部分,这是我要更改为更优雅的部分:

@Configuration
public class MyAppConfiguration{
    @Bean 
    public Registry providerRegistry(){
        Registry reg = new Registry();
        reg.register(new Provider1());
        reg.register(new Provider2());
        return reg;
    }
}

Then I can inject the registry into other classes that need to operate on the services. 然后,我可以将注册表注入需要在服务上进行操作的其他类中。

I know that DI is for providing different implementation for a single type but one at a time. 我知道DI用于为一种类型提供不同的实现,但一次只能提供一种。 So DI most probably is not the right tool, my question is more about spring if there's a good way I don't know to achieve this. 因此,DI很可能不是正确的工具,我的问题更多是关于spring,如果我不知道有什么好方法可以实现这一目标。 As example Annotate them with a Qualifier and then resolve all Beans with that qualifier in the registry. 例如,用限定符注释它们,然后在注册表中解析所有具有该限定符的Bean。

The way I would not want to take is using a custom Annotation and then resolving all the classes via reflection, instantiate them and put them in the registry. 我不想采用的方法是使用自定义注释,然后通过反射来解析所有类,实例化它们并将它们放入注册表中。 But at the moment it's the only way I can see so I don't have to modify the config and handwire the services. 但是目前,这是我看到的唯一方法,因此无需修改配置并手动连接服务。

If you want all of them to be autowired, you can just autowire a list of your interface implementations. 如果您希望它们全部都自动接线,则只需自动接线接口实现列表即可。

@Bean
public class Provider1 implements DataProvider { ... }

@Bean
public class Provider2 implements DataProvider { ... }

@Bean
public class Registry{
    @Autowired
    private List<DataProvider> providers;

    public List<DataProvider> getProviders(){
       //return providers
    }

    public Data someAggregatedOperation(){ ... }
}

In case you want to keep using a configuration class you could do something like: 如果您想继续使用配置类,可以执行以下操作:

@Bean
public class Provider1 implements DataProvider { ... }

@Bean
public class Provider2 implements DataProvider { ... }

public class Registry{
    private List<DataProvider> providers;

    public Registry(final List<DataProvider> providers) {
      this.providers = providers;
    }

    public List<DataProvider> getProviders(){
       //return providers
    }

    public Data someAggregatedOperation(){ ... }
}

@Configuration
public class MyAppConfiguration{

    @Bean
    @Autowired
    public Registry providerRegistry(final List<DataProvider> providers){
        return new Registry(providers);
    }
}

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

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