简体   繁体   English

注入实例 <Interface> :Spring和CDI兼容性

[英]Inject Instance<Interface> : Spring and CDI compatibility

I am wondering how can I use the Instance in JUnit4 with Spring 我想知道如何在Spring中使用JUnit4中的实例

@Inject
Instance<IMyInterface> interfaces;

If I use 如果我用

@Inject
List<IMyInterface> interfaces;

It works in Spring but not with CDI. 它在Spring中有效,但不适用于CDI。
Also, we can use Provider with both CDI and Spring but it's not Iterable. 另外,我们可以将Provider与CDI和Spring一起使用,但是它不是可迭代的。

The @Inject annotation comes from JSR-330-Dependency Injection for Java. @Inject批注来自Java的JSR-330-Dependency Injection。 Spring knows this annotation and briefly said, Spring treats it as an alternative to @Autowired . Spring知道此注释,并简要地说,Spring将其视为@Autowired的替代方法。 That's it. 而已。

However, the Instance is part of JSR 299 - Contexts & Dependency Injection. 但是,该实例是JSR 299-上下文和依赖注入的一部分。 You can have a look at the definition in CDI specifications. 您可以查看CDI规范中的定义

Spring DI is absolutely different and does not implement JSR-299 (CDI) or any other standard. Spring DI完全不同,并且不实现JSR-299(CDI)或任何其他标准。 It does not even have a separate API and implementations and everything is just glued together. 它甚至没有单独的API和实现,所有内容都粘合在一起。 Therefore, injecting an Instace is not possible with Spring. 因此,Spring 无法注入Instace。

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

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