简体   繁体   中英

Inject Instance<Interface> : Spring and CDI compatibility

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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