注入实例<接口> :Spring和CDI兼容性 [英] Inject Instance<Interface> : Spring and CDI compatibility

查看:92
本文介绍了注入实例<接口> :Spring和CDI兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用实例在带有Spring的JUnit4中

I am wondering how can I use the Instance in JUnit4 with Spring

@Inject
Instance<IMyInterface> interfaces;

如果我使用

@Inject
List<IMyInterface> interfaces;

它在Spring上有效,但在CDI上不起作用。

另外,我们可以使用提供程序同时具有CDI和Spring,但它不可迭代。

It works in Spring but not with CDI.
Also, we can use Provider with both CDI and Spring but it's not Iterable.

推荐答案

@Inject 批注来自JSR-330依赖注入对于Java。 Spring知道此注释,并简短地说,Spring将其作为 @Autowired 的替代方法。就是这样。

The @Inject annotation comes from JSR-330-Dependency Injection for Java. Spring knows this annotation and briefly said, Spring treats it as an alternative to @Autowired. That's it.

但是,该实例是JSR 299的一部分-Contexts&依赖注入。您可以查看CDI中的定义

However, the Instance is part of JSR 299 - Contexts & Dependency Injection. You can have a look at the definition in CDI specifications.

Spring DI完全不同,并且没有实现JSR-299(CDI)或任何其他标准。它甚至没有单独的API和实现,所有内容都粘合在一起。因此,用Spring注入Instace是不可能的

Spring DI is absolutely different and does not implement JSR-299 (CDI) or any other standard. It does not even have a separate API and implementations and everything is just glued together. Therefore, injecting an Instace is not possible with Spring.

这篇关于注入实例&lt;接口&gt; :Spring和CDI兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆