简体   繁体   English

Spring:注入实现接口的所有具体类的列表

[英]Spring: Injecting list of all concrete classes implementing an interface

I have an @Service class which contains a List of other @Service classes.我有一个@Service class ,其中包含其他@Service类的List That List basically contains all the services which implements UniformEventsResearchApi .List基本上包含所有implements UniformEventsResearchApi的服务。

Being a rookie with Spring, I'm not sure how I can get Spring to allow me to follow the Open-Closed Principle and thus having that list automagically injected with all of the concrete implementations.作为 Spring 的新手,我不确定如何让 Spring 允许我遵循开闭原则,从而使该列表自动注入所有具体实现。

Here is an incomplete UML class diagram:这是一个不完整的 UML class 图:

uml类图

And here is some "code":这是一些“代码”:

@Service
public class EventsResearchService {

    // todo: this should be Injected automatically
    private List<UniformEventsResearchApi> eventsResearchApis = Arrays.asList(new EventbriteApi());

// Already tried, but without success:
//
//    @Autowired
//    private List<UniformEventsResearchApi> eventsResearchApis2;
//
//    @Autowired
//    @Qualifier("EventsResearchApi")
//    public void setXList(List<UniformEventsResearchApi> apis) {
//        this.eventsResearchApis2 = apis;
//    }

}


@Service
@Qualifier("EventsResearchApi")
public interface UniformEventsResearchApi { /* ... */ }


public abstract class EventsResearchApi implements UniformEventsResearchApi { /* ... */ }


/** Any class like this one which extends EventsResearchApi should be automatically injected in the List */
public class EventbriteApi extends EventsResearchApi { /* ... */ }

This is an easy task for spring actually:实际上,这对 spring 来说是一项简单的任务:

You can auto wire a list of beans just like a regular bean In this case spring will indeed find all the beans that implement the interface and inject:您可以像普通 bean 一样自动连接 bean 列表。在这种情况下,spring 确实会找到所有实现接口并注入的 bean:

public interface SomeInterface {
}

@Component
public class SomeImpl1 implements SomeInterface {}

@Component
public class SomeImpl2 implements SomeInterface {}

@Service
public сlass SampleBean {

     @Autowired
     private List<SomeInterface> beans;
}

One note, there should be at least one implementation of beans available, Otherwise Spring won't let you such an injection.需要注意的是,至少应该有一个可用的 bean 实现,否则 Spring 不会让你进行这样的注入。 If you know that such a situation is possible you can inject Optional<List<SomeInterface>>如果您知道这种情况是可能的,您可以注入Optional<List<SomeInterface>>

With field injection it looks rather ugly, but you can use constructor injection (which is better anyway) or consider using “java configuration”:使用字段注入它看起来相当丑陋,但您可以使用构造函数注入(无论如何更好)或考虑使用“java配置”:

@Service
public class SampleBean {
   private final List<SomeInterface> beans;
   // I haven’t checked this with compiler, should work
    @Autowired // optional, if you have a single constructor, you can omit it
    public SampleBean(Optional<List<SomeInterface>> beans) {
       this.beans = beans.orElse(Collections.emptyList());   
    }
}

暂无
暂无

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

相关问题 如何不允许对所有类使用我自己的注释,而不是对实现具体接口的类进行使用 - How can I disallow to use my own annotation for all classes instead of classes implementing concrete interface 实现接口的类列表 - List of Classes implementing an Interface 如何获取从具有指定泛型(接口)的接口继承的所有类的列表<concrete class> )</concrete> - How get list of all classes that inherit from the interface with the specified generic (interface<concrete class>) 如何正确组织一组在不同包中实现接口的具体类? - How to correctly organize a set of concrete classes implementing an interface in different packages? 在两个 bean 中注入 spring bean-实现相同的接口 - Injecting spring bean among two beans- implementing same interface 获取实现给定接口的所有类 - Get all classes implementing given interface AfterAdvice,用于静态实现接口的所有类的构造函数 - AfterAdvice for all constructors of classes statically implementing an interface 创建实现接口的所有类的向量 - Create vector of all classes implementing an interface Java函数,带有将接口实现为参数的类的列表 - Java function with list of classes implementing interface as argument 处理在此 Spring Boot 应用程序中实现接口的不同类列表的智能解决方案是什么? - What is a smart solution to handle a list of different classes implementing an interface in this Spring Boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM