If my object is produced by a producer method, will its initializer methods be called automatically?

StackOverflow https://stackoverflow.com/questions/10920455

  •  13-06-2021
  •  | 
  •  

문제

I have a producer method that creates instances of—let's say—Person.

The instance that this method creates is of type PersonBean and quite obviously implements Person.

This class has an initializer method in it:

@Inject
public void setSomething(final Something something) {
  // whatever
}

There is also a Something implementation on the classpath in a bean archive that should be a candidate for injecting here.

I've observed that this implementation is injected into an appropriately-annotated field of another non-producer-method-produced object, so I know Weld is finding it, it's valid, etc. etc.

The specification is not clear (to me) on whether or not my producer method's return value will then also have Weld/container-provided dependency injection applied to it.

I am certainly observing that this initializer method is not called automatically.

Boiled down, my question is: if I am producing an object, then does that mean my producer method must take care of all initialization of that object, or (what I'd expect instead) is my producer method only responsible for instantiation?

도움이 되었습니까?

해결책

if I am producing an object, then does that mean my producer method must take care of all initialization of that object, or (what I'd expect instead) is my producer method only responsible for instantiation?

From the spec:

3.3. Producer methods
    A producer method acts as a source of objects to be injected, where:
    - the objects to be injected are not required to be instances of beans, 

This should answer your question: If you are instantiating a new object (using new) you will have to take care of the initialization.

(A probably more common usecase of producer methods is to configure and produce an existing bean into a certain scope...)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top