Java 不明确的setter方法导致赋值问题

Java 不明确的setter方法导致赋值问题,java,spring,javabeans,configuration-files,Java,Spring,Javabeans,Configuration Files,当设置具有不明确setter方法的bean的属性时,我遇到了一个问题 问题在于HttpClient的hostConfiguration.host属性有3种可能的使用方法: HostConfiguration.setHost(字符串主机) HostConfiguration.setHost(HttpHost主机) setHost(URI主机) 以下是我的bean定义: <bean id="primaryClient" class="org.apache.commons.httpclient.

当设置具有不明确setter方法的bean的属性时,我遇到了一个问题

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

  • HostConfiguration.setHost(字符串主机)
  • HostConfiguration.setHost(HttpHost主机)
  • 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>
    
    
    
    以下是堆栈:

    原因:org.springframework.beans.factory.BeanCreationException:创建名为“primaryClient”的bean时出错,该bean在类路径资源[spring/test-merchantlink context.xml]中定义:设置属性值时出错;嵌套异常为org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessException(1)是: PropertyAccessException 1:org.springframework.beans.typemischException:未能将[org.apache.commons.httpclient.HttpHost]类型的属性值转换为属性“hostConfiguration.host”所需的类型[java.lang.String];嵌套异常为java.lang.IllegalArgumentException:无法将[org.apache.commons.httpclient.HttpHost]类型的值转换为属性“host”所需的[java.lang.String]类型:找不到匹配的编辑器或转换策略


    我怎样才能避开这件事?我使用的是Spring2.5.6

    您应该避免重载希望能够使用IoC连接的属性设置程序。当(逻辑上)同一属性确实需要两个setter时,应该使用不同的setter名称。。。和javadoc注释来解释发生了什么

    上有相同的问题描述了相同的问题,更好的解决方案是重命名所有方法


    基本上,重载setter违反了JavaBeans规范,因此spring不支持。您可以查看更多详细信息

    由于您可能没有创建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

    中,似乎必须显式指定预期的类名,以避免多个可用设置器之间出现歧义

    可能是这样的:

    <bean id="primaryClient" class="org.apache.commons.httpclient.HttpClient">
        <property name="hostConfiguration.host">
            <bean class="org.apache.commons.httpclient.HttpHost" >
                <constructor-arg type="java.lang.String">
                    <value>somelink.com</value>
                </constructor-arg>
                <constructor-arg type="int">
                    <value>443</value>
                </constructor-arg>
                <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>
    
    
    somelink.com
    443
    
    没有。那没用。设置hostConfiguration.host属性时,问题更严重。不在HttpHost bean上。我已经单独创建了一个,在HttpClient bean定义范围之外没有任何问题。谢谢。这是有道理的,但是我认为Spring足够智能,可以根据传入的参数类型确定要调用哪个方法。我同意。他们的理由似乎是向后兼容并遵守规范。