问题
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