@Inject only working for POJOs created by CDI container?

前端 未结 5 1334
终归单人心
终归单人心 2020-12-17 21:44

I just want to confirm that I fully understood the prerequisites for CDI to work. If I have a class A:

public class A {
    @Inject private B b;
}

5条回答
  •  不知归路
    2020-12-17 22:23

    Does CDI only work if the class requiring an injection was also created by CDI container?

    Yes, that's pretty much it. The lifecycle of a ManagedBean is controlled by the container and should never be instantiated with the new keyword (BTW: the same is true for EJBs & Spring beans). If you need to create a new ManagedBean you will probably want to use a producer method.

提交回复
热议问题