Spring 3当setter方法重载时如何设置属性

发布于 2024-09-28 20:17:24 字数 510 浏览 3 评论 0原文

class SomeoneElsesAPI {
   void setDirectory(File dir){...}
   void setDirectory(String dir){...}
}

<bean id="someoneElsesAPI" class="com.someone.SomeoneElsesAPI">
   <property name="directory">
      <value type="java.lang.String">/etc</value>
   </property>
</bean>

我有一个与上面的例子类似的情况,Spring 抛出一个 IllegalStateException 表明它无法将 String 转换为 File。

我如何告诉 spring 使用哪个重载方法?我认为当我在 参数中指定它时,spring 会弄清楚它。

class SomeoneElsesAPI {
   void setDirectory(File dir){...}
   void setDirectory(String dir){...}
}

<bean id="someoneElsesAPI" class="com.someone.SomeoneElsesAPI">
   <property name="directory">
      <value type="java.lang.String">/etc</value>
   </property>
</bean>

I have a case similar to the above example, Spring is throwing an IllegalStateException indicating that it can't convert String to File.

How do I tell spring which of the overloaded methods to use? I thought spring would figure it out when I specified it in the <value type="..."> parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-10-05 20:17:24

我不知道这是否是一个令人满意的答案,但我避免重载我希望能够使用 IoC 连接的属性设置器。当我确实需要两个 setter 来(逻辑上)相同的属性时,我使用不同的 setter 名称...和 ​​javadoc 注释来解释发生了什么。

I don't know if this is a satisfactory answer, but I avoid overloading property setters that I want to be able to wire using IoC. When I do need two setters for (logically) the same property, I use different setter names ... and javadoc comments to explain what is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文