我正在尝试Spring注入org.apache.http.impl.conn.PoolingHttpClientConnectionManager和org.apache.http.config.SocketConfig。我试图设置默认的连接数,最大连接数和soTimeout值。我们使用的是apache版本4.1,1。
在spring初始化时,我收到一个错误,指示soTimeout和tcpNoDelay不是可写属性。从我的所有Google搜索中,我发现这些属性应该是可写的。有人可以帮我理解Apache 4.3.1上的HTTP客户端初始化
的applicationContext.xml
<bean id="dispatcherHttpConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<!-- property name="params" ref="dispatcherHttpConnectionParams" /-->
<property name="maxTotal" value="1000" />
<property name="defaultMaxPerRoute" value="50" />
<property name="defaultSocketConfig" ref="apacheSocketConfig" />
身份验证/授权失败,错误为org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatcherHttpConnectionManager' defined in class path resource [dispatchClientApplicationContext.xml]: Cannot resolve reference to bean 'apacheSocketConfig' while setting bean property 'defaultSocketConfig'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apacheSocketConfig' defined in class path resource [dispatchClientApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'soTimeout' of bean class [org.apache.http.config.SocketConfig$Builder]: Bean property 'soTimeout' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
答案 0 :(得分:0)
老问题我知道,但是......
您需要使用SocketConfig.Builder(http://javadox.com/org.apache.httpcomponents/httpcore/4.3/org/apache/http/config/SocketConfig.Builder.html)来构造SocketConfig bean:
<bean id="socketConfigBuilder" class="org.apache.http.config.SocketConfig" factory-method="custom">
<property name="soTimeout" value="1000" />
</bean>
<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="defaultSocketConfig"><bean factory-bean="socketConfigBuilder" factory-method="build" /></property>
</bean>