Inject Instance<Interface> : Spring and CDI compatibility

≯℡__Kan透↙ 提交于 2019-12-10 12:01:28

问题


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

@Inject
Instance<IMyInterface> interfaces;

If I use

@Inject
List<IMyInterface> interfaces;

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


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/44458750/inject-instanceinterface-spring-and-cdi-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!