Why can we have only one constructor with @Inject annotation?

1.5k Views Asked by At

I know the above is mandated by the spec but would like to understand why? What will be the problem(s) if multiple constructors in a class are allowed to have @Inject annotation. I am talking about JSR 330.

P.S: I know CDI spec(ie JSR 299) leverages JSR 330's @Inject. So I am adding that tag in the question as well. Feel free to remove if it doesn't apply to CDI.

1

There are 1 best solutions below

5
On BEST ANSWER

If multiple constructors are annotated with @Inject, there will be ambiguity for the container to decide which to use to instantiate the type.

Which would you choose

@Inject
public Foo (Bar bar) {}

or

@Inject 
public Foo (Zoom zoom) {}

if there is absolutely no relation between Bar and Zoom.