如何使用factory-method在Spring中创建SocketConfig类?

时间:2014-09-18 00:18:07

标签: spring apache-httpclient-4.x spring-3

我正在使用apache commons httpclient 4.3.x和spring3。我正在尝试使用它的相关socketconfig实例连接连接池。

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.html

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/config/SocketConfig.html?is-external=true

我的代码如下所示:

<bean id="socketConfig" class="org.apache.http.config.SocketConfig" factory-method="custom" init-method="build">
        <property name="soTimeout" value="60000"/>
        <property name="soLinger" value="5" />
</bean>

<bean name="poolingHttpConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" depends-on="socketConfig">
        <property name="maxTotal" value="20" />
        <property name="defaultMaxPerRoute" value="20" />
        <property name="defaultSocketConfig" ref="socketConfig" />
</bean>

但是,这不起作用。用于PoolingHttpClientConnectionManager上的setDefaultSocketConfig()的实例类型是SocketConfig.Builder类型,而不是SocketConfig。

我想要发生的事情如下:

SocketConfig config = SocketConfig.custom()
 .setSoTimeout(60000)
 .setSoLinger(5)
 .build()

所以,我希望socketConfig bean类型应该是SocketConfig实例,而不是SocketConfig.Builder实例。

根据春季文档,我认为这应该有用。

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-factory-class-static-factory-method

有什么我做错了吗?或者春天不支持这个?

2 个答案:

答案 0 :(得分:0)

事实证明,socketconfig构建器实例不适用于spring。

我必须使用spring beanfactory实现来创建实例。

bean类:

import org.apache.http.config.SocketConfig;
import org.springframework.beans.factory.FactoryBean;

public class SocketConfigFactoryBean implements FactoryBean<SocketConfig> {
    int soLinger;
    int soTimeout;

    public SocketConfig getObject() throws Exception {
        return SocketConfig.custom()
                .setSoLinger(soLinger)
                .setSoTimeout(soTimeout)
                .build();
    }

    public Class<?> getObjectType() {
        return SocketConfig.class;
    }

    public boolean isSingleton() {
        return true;
    }

    public int getSoLinger() {
        return soLinger;
    }

    public void setSoLinger(int soLinger) {
        this.soLinger = soLinger;
    }

    public int getSoTimeout() {
        return soTimeout;
    }

    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
    }
}

bean定义

<bean name="poolingHttpConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <property name="maxTotal" value="20" />
    <property name="defaultMaxPerRoute" value="20" />
    <property name="defaultSocketConfig">
        <bean class="org.apache.http.config.SocketConfig" factory-method="custom" init-method="build">
        <bean class="com.ex.spring.beans.factory.SocketConfigFactoryBean">
            <property name="soTimeout" value="60000"/>
            <property name="soLinger" value="5" />
        </bean>
    </property>
</bean>

答案 1 :(得分:0)

我能够通过在春天进行下一次配置来实现它:

<bean id="socketConfig" class="org.apache.http.config.SocketConfig" factory-method="custom">
    <property name="soTimeout" value="1000" />
    <property name="soLinger" value="5" />
</bean>

<bean name="poolingHttpConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <property name="maxTotal" value="20" />
    <property name="defaultMaxPerRoute" value="20" />
    <property name="defaultSocketConfig">
        <bean factory-bean="socketConfig" factory-method="build" />
    </property>
</bean>