简体   繁体   中英

CDI @Inject Instance @Any - But instance never populated with instances

Why does 'instance' never iterate over any implementations? What am I missing?

JBoss EAP 6.3.0.GA (AS 7.4.0.Final-redhat-19)

public interface Simple { }
public class SimpleA implements Simple { public SimpleA() { } }
public class SimpleB implements Simple { public SimpleB() { } }
public class SimpleUser {
    @Inject @Any Instance<Simple> instance;

    @PostConstruct public void init() {
        for (final Simple simple : instance) {
            System.out.println(simple);
        }
    }
}

In case it helps someone else, I also am using Deltaspike 1.5.2 and am running into the same issue (if I remove Deltaspike, I no longer have the problem).

In my case adding producer methods did not solve it. After looking around I BeanProvider which gets around the problem but is far from elegant.

https://deltaspike.apache.org/documentation/core.html#BeanProvider

I had to call

List<MyServiceInterface> myServiceList = BeanProvider.getContextualReferences(MyServiceInterface.class, false, false);

There are probably better ways. I notice Deltaspike turns on a bunch of extensions by default, but couldn't find docs on how to disable the ones I am not using as I suspect one may be causing this issue.

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