gpt4 book ai didi

java - c-命名空间和类型转换

转载 作者:行者123 更新时间:2023-12-01 05:25:47 24 4
gpt4 key购买 nike

我有一个带有 4 个整数参数的构造函数的类。它在 spring xml 中配置如下:

<bean id="mySearchService" class="app.service.MySearchService"
c:maxConnectionsPerHost="${maxConnectionsPerHost}"
c:maxTotalConnections="${maxTotalConnections}"
c:connectionTimeout="${connectionTimeout}"
c:soTimeout="${soTimeout}" />

我的属性文件有:

maxConnectionsPerHost=50
maxTotalConnections=50
connectionTimeout=500
soTimeout=100

但我收到以下错误:

Error creating bean with name 'mySearchService' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.lang.Integer]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

如果我使用<constructor-arg value="..." />,这有效所以我想知道 c 命名空间是否不以与构造函数参数相同的方式进行类型转换。

最佳答案

The documentation提到:

The p-namespace is not as flexible as the standard XML format. For example, the format for declaring property references clashes with properties that end in Ref, whereas the standard XML format does not.

还有:

The c: namespace uses the same conventions as the p: one (trailing -ref for bean references) for setting the constructor arguments by their names.

据此,我推断 - 尽管文档似乎从未如此明确地说过 - 你实际上需要编写:

<bean id="mySearchService" class="app.service.MySearchService"
c:max-connections-per-host="${maxConnectionsPerHost}"
c:max-total-connections="${maxTotalConnections}"
c:connection-timeout="${connectionTimeout}"
c:so-timeout="${soTimeout}" />

使用连字符属性名称而不是驼峰命名法。

关于java - c-命名空间和类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com