-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In Bean definition by XMLConfig, class constructor selection may be random #31871
Comments
Could you share a minimal sample project that demonstrates the problem? Can you replicate the issue with 6.1.0-M1 as well? This could be linked to #29559 and this note about parameter name retention in the upgrade guide. It doesn't look like httpcomponents is compiling their code with the |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue. |
I apologize for the delayed response. I have created a sample (sample-constructor.zip) to demonstrate this problem. This sample uses Spring 6.1.4, but the same problem occurs with Spring 6.1.14. In this sample, as you pointed out, when using HttpHost compiled from source code with the -parameters option, the correct constructor is used. However, HttpHost is not a class from our application but is a class provided by a third-party library, Apache HttpComponents Core. Normally, such third-party libraries are obtained from repositories like Maven Central Repository, and users of Apache HttpComponents Core typically do not build them from source code. I understand that building from the source code with the -parameters option resolves the problem, but currently, it appears that the Spring Framework assumes that not only user applications but also third-party libraries are always compiled with the -parameters option. Since the default behavior of javac does not include -parameters, I believe this assumption should not be made. Is this situation intended by the Spring Framework? if compiling with the -parameters option is indeed the intended assumption, does it mean that in Bean definitions using XMLConfig, to address this problem for third-party libraries built without the -parameters option, we need to specify the type attribute or arg-index attribute? |
Affects: version 6.1.0-M3 or later
Summary
In XMLConfig, if a bean-defined class has an overloaded constructor, an exception may occur at runtime.
I checked the logs and found that the wrong constructor was used.
This behavior seems to occur when only the name attribute is used on the constructor-arg element.
It does not occur when the type or index attribute is used on the constructor-arg element.
Detail
I found this behavior in the bean definition of RestTemplate.
Here is an example of a Baen definition that throws an exception.
I was expecting
HttpHost(final String hostname, final int port)
to be used in the above example.
But actuality
HttpHost(final String scheme, final String hostname)
was used.
https://github.com/apache/httpcomponents-core/blob/master/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java#L118-L135
The log of that case is shown below.
log.txt
On the other hand, when I wrote a bean definition with the type attribute
HttpHost(final String hostname, final int port)
is used.
I think that within the constructor-arg element, the name attribute has not worked version 6.1.0-M3 or later.
Is this a Spring Framework spec change or a bug?
The text was updated successfully, but these errors were encountered: