Suppose I have two classes: first a class without any properties, fields or annotations:
public class B {}
And the class that B introduces is like this:
public class A { @Inject private B b; public B getB() { return b; } }
Now class A is pretty useless until we use it, so there are two options:
- @ Paste it
- Create it manually using the robust "new A ()"
If A receives an injection, the CDI controls it and is kind enough to introduce B, which has an implicit @Dependent scope. Cool, just what I want.
However, if I manually build A (say, in a factory or builder), CDI completely ignores my object and will not introduce an object of type B.
Example: I am talking about when this does not work, here the object a will always remain zero:
public class Builder { @Inject private A a; public static Builder ofTypeSomething() {
Why is this not working?
Class A is a valid managed bean and has a valid scope, like class B. Even if I add @Producer to a static method, it will not change anything (this is good, because the idea of a static method is to call it, and not insert Builder in any location).
Mythica
source share