Suppose that I have two classes, first a class without any properties, fields or annotations:

public class B {}


And a class which gets B injected, like this:

public class A {
    private B b;

    public B getB() {
        return b;


Now class A is pretty useless until we use it, so there are two options:

  • @Inject it

  • 使用可靠的手动构建它新的A()


If A gets injected, CDI manages it and is kind enough to inject B which has the implicit scope of @Dependent. Cool, just what I want.


However, if I manually construct A (let's say in a factory or a builder), CDI completely ignores my object and won't inject an object of type B.


Example I'm talking about when it doesn't work, here object a will always remain null:

public class Builder {
    private A a;

    public static Builder ofTypeSomething() {
        // do some magic here
        return new Builder();

    private Builder() {
        // and some more here



Class A is a valid managed bean and has a valid scope, just like class B. Even if I add @Producer to the static method, it won't change anything (which is fine, cause the idea of the static method is to call it, not to inject Builder anywhere).

 1 条回答