Spring 3.2 annotation autowiring with multiple constructors
Spring 5 docs:
Only one annotated constructor per-class can be marked as required, but multiple non-required constructors can be annotated. In that case, each is considered among the candidates and Spring uses the greediest constructor whose dependencies can be satisfied, that is the constructor that has the largest number of arguments.
Although one constructor is required, spring might use the other constructors if dependancies are sesolved;
I think the reason for that behavior is that if one of the constructors have @Autowired(required=true)
then it must be called (because it is required) and because only one constructor can be called per object instantiation then what is the point of having other constructors with @Autowired(required=false)
?
They won't be autowired anyway, because one of the constructors is already required and must be called. They can still be called without using Autowired mechanism, but in that case @Autowired(required=false)
annotation is unnecessary.