I found that during Bean instantiation, the resolvedConstructorArguments
are always empty or null.
The purpose of using the cache parameter is to quickly find the corresponding method when the Bean is instantiated the second time. However, in order to avoid creating multiple prototype beans whose properties reference the same object, we cannot directly cache the parameters to be used by the method, so the preparedConstructorArguments
parameter appears.
As far as I can tell, resolvedConstructorArguments
is either null or an empty array, and it's only going to be an empty array if the following if-condition is satisfied. The rest of the time it is null.
In ConstructorResolver
, its instantiateUsingFactoryMethod
and autowireConstructor
methods contain the following code:
if (candidates.length == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
Constructor<?> uniqueCandidate = candidates[0];
if (uniqueCandidate.getParameterCount() == 0) {
synchronized (mbd.constructorArgumentLock) {
mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
mbd.constructorArgumentsResolved = true;
mbd.resolvedConstructorArguments = EMPTY_ARGS;
}
bw.setBeanInstance(instantiate(beanName, mbd, uniqueCandidate, EMPTY_ARGS));
return bw;
}
}
When resolvedConstructorArguments
is an empty array, it looks like resolvedConstructorArguments
's job is to make sure that no arguments are needed when the method is called.
I don't understand why it's designed this way.
The resolvedConstructorArguments
field doesn't seem necessary.
Comment From: JulianHang
I also want to know the explanation of this question,thanks! @spring-issuemaster
Comment From: daimingzhi
My guess is that in order to expand, we can through some way directly to assign some value to "resolvedConstructorArguments"。 but I can not sure, hope someone can give the answer
Comment From: snicoll
resolvedConstructorArguments
is a package-visible field that's access by ConstructorResolver
and is necessary.