java - 不明确的 setter 方法导致分配属性值出现问题

标签 java spring javabeans configuration-files

我在设置具有不明确 setter 方法的 bean 的属性时遇到问题。

问题是 HttpClient 的 hostConfiguration.host 属性有 3 种可能的使用方法:

  1. HostConfiguration.setHost(字符串主机)
  2. HostConfiguration.setHost(HttpHost 主机)
  3. HostConfiguration.setHost(URI 主机)

这是我的 bean 定义:

<bean id="primaryClient" class="org.apache.commons.httpclient.HttpClient">
    <property name="hostConfiguration.host">
        <bean class="org.apache.commons.httpclient.HttpHost" >
            <constructor-arg value="somelink.com"/>
            <constructor-arg value="443"/>
            <constructor-arg>
                <bean class="org.apache.commons.httpclient.protocol.Protocol">
                    <constructor-arg value="https"/>
                    <constructor-arg ref="sslProtocolSocketFactory"/>
                    <constructor-arg value="443"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>

这是堆栈:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'primaryClient' defined in class path resource [spring/test-merchantlink-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.apache.commons.httpclient.HttpHost] to required type [java.lang.String] for property 'hostConfiguration.host'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.apache.commons.httpclient.HttpHost] to required type [java.lang.String] for property 'host': no matching editors or conversion strategy found

我该如何解决这个问题?我正在使用 spring 2.5.6

最佳答案

基本上,重载 setter 违反了 JavaBeans 规范,因此不受 spring 支持。可以看到https://jira.spring.io/browse/SPR-4931了解更多详情。

由于您可能没有创建 HttpClient,因此无法更改它,因此一种解决方案是使用工厂 bean。这看起来像这样:

<bean id="httpClientFactory" class="my.package.HttpClientFactory"/>

<bean id="primaryClient" factory-bean="httpClientFactory" factory-method="create">

您可以在 my.package.HttpClientFactor 上实现 create() 方法来设置和返回您的工厂。

如果您想在 XML 配置中而不是在代码中设置特定参数,您可以将构造函数参数添加到 my.package.HttpClientFactor

关于java - 不明确的 setter 方法导致分配属性值出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25335302/

相关文章:

java - 如何使用匹配器在 TextView 中隐藏#

java - 如何正确扩展和实现Java泛型接口(interface)

spring - 配置为监听端口 8080 的 Tomcat 连接器失败

java - Spring JPA中无法使用多线程从多个数据库中删除数据

java - 解析 SVG - java.net.MalformedURLException : no protocol: <? xml 版本 ="1.0"编码 ="UTF-8"独立 ="no"?>

javascript - 从 javascript 调用 bean 函数

java - Setter 方法在 JSF 中不起作用

java - 如何使用接口(interface)在 Activity 和 sqlite 数据库助手类之间进行通信

java.lang.illegalstateException 无法转发。响应已提交 jsp

java - 尝试使用 spring 部署 liferay portlet 时出现异常