【问题标题】:How to create a SocketConfig class in Spring using factory-method?如何使用工厂方法在 Spring 中创建 SocketConfig 类?
【发布时间】:2014-09-18 00:18:07
【问题描述】:

我正在使用 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 实例。

根据 spring 文档,我认为这应该可行。

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

我做错了什么吗?或者这只是在春季不支持?

【问题讨论】:

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


    【解决方案1】:

    事实证明,socketconfig builder 实例并非设计用于很好地与 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>
    

    【讨论】:

      【解决方案2】:

      我可以通过在 spring 中进行下一个配置来实现它:

      <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>
      

      【讨论】:

        猜你喜欢
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多