TL;DR: You can't. At least not using the CDI standard. Maybe not even in Weld, not sure. But here's what you can do:


Sometimes you might want to create an app which is tolerant to unsatisfied CDI dependencies.

For example, you might want to have optional secondary MailSession which could be used as a fallback.

Basically, any time you want to have something optional and not bother having an extra option like "enableFoo = false".


So instead of getting an exception from the deepness of your application server, you would like to detect in your app whether the CDI dep was satisfied or not. Ideally, by having it set to null.

For example:


@Inject(nullOnFailure=true) FooBean foo;


Well, there's no such feature in CDI.

But there's something you can use:


class Bar {

    @Inject Instance<FooBean> foo;

    public doSomething(){
        if( foo.isUnsatisfied() ){
             log.error("Foo was not found!")