Search code examples
cdi

CDI @Dependent and @New


I've two fields in my class:

public class Class {

    @Inject private ClassA a;
    @Inject private ClassB b;

    public void search(String lqlSentence)
    {
        this.a.something();
        AnotherClass anotherOne = new AnotherOne;
        anotherOne.method(this.b);
    }
}

CDI tells me:

Unsatisfied dependencies for type ClassA with qualifiers @Default

However, CDI tells me anything about ClassB b field.

Then, I've added a @Dependent annotation:

@Inject @Dependent private ClassA a;

CDI tells me the same.

However, if I annotate this field with a @New CDI works.

Why if I use @New works? Why CDI doesn't tell me something about the other property Class B?


Solution

  • This qualifier annotation (@Dependent) go on the class definition itself, or the producer for the bean. Not on the injection point.

    Also, don't use @New. its deprecated. It functions the same as dependent, but is driven from the injection point side whereas CDI is focused on the producer side.