1

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.

Charles Follet
  • 827
  • 1
  • 10
  • 28

1 Answers1

0

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.

  • So there is no workaround except injecting all classes one by one using @Inject? – Charles Follet Jun 13 '17 at 08:50
  • When Spring core is used, then there is no official way. There are some projects that may help you: https://cwiki.apache.org/confluence/display/DeltaSpike/Spring+CDI+integration https://github.com/matzew/spring-cdi-bridge –  Jun 13 '17 at 15:43