我将此部分代码作为示例:
public SapRestClient(final String scheme, final String host,
final int port, final String username, final String password,
final HttpClientConnectionManager connectionManager,
final RequestConfig requestConfig) {
final BasicCredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(authscope, credentials);
final CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig).build();
final HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
final BufferingClientHttpRequestFactory bufferingRequestFactory =
new BufferingClientHttpRequestFactory(requestFactory;
this.restTemplate = new RestTemplate(bufferingRequestFactory);
我会在Spring做这样的事情。我写了这段代码:
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
factory-method="custom">
<property name="socketTimeout" value="5000" />
<property name="connectTimeout" value="5000" />
</bean>
<bean id="requestConfig" factory-bean="requestConfigBuilder"
factory-method="build" />
<bean id="connectionManager"
class="org.apache.http.conn.HttpClientConnectionManager">
<property name="maxTotal" value="30" />
<property name="defaultMaxPerRoute" value="50" />
</bean>
<bean id="credentialsProvider" class="org.apache.commons.httpclient.auth.CredentialsProvider"/>
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="defaultRequestConfig" ref="requestConfig" />
<property name="defaultConnectionManager" ref="connectionManager"/>
<property name="defaultCredentialsProvider" ref="credentialsProvider"/>
</bean>
<bean id="httpClient" factory-bean="httpClientBuilder"
factory-method="build" />
<bean id="requestFactory" factory-bean="httpComponentsClientHttpRequestFactory">
<property name="defaultHttpClient" ref="httpClient"/>
</bean>
<bean id="bufferingRequestFactory" factory-bean="bufferingClientHttpRequestFactory">
<property name="defaultRequestFactory" ref="requestFactory"/>
</bean>
<bean id="bufferingClientHttpRequestFactory" class="org.springframework.http.client.BufferingClientHttpRequestFactory"/>
<bean id="twRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<ref bean="bufferingRequestFactory" />
</constructor-arg>
<property name="errorHandler" ref="customErrorHandler" />
</bean>
我在一个closeableHttpClient中放置了一个connectionManager(带有连接设置)和一个请求配置器来进行超时管理(我看到了这个链接)。我把这个bean放在HttpComponentClientHttpRequestFactory中,然后放在BufferingRequestFactory中,最后放在RestTemplate中。
但是在运行时我得到了这个错误:
WARN [localhost-startStop-1] [CloseAwareApplicationContext]异常 在上下文初始化期间遇到 - 取消刷新 尝试:org.springframework.beans.factory.BeanCreationException: 创建名为'defaultTwProductSearchService'的bean时出错 在类路径资源[jkdsfj-spring.xml]中定义: 无法解析bean'twNavigatorClientGateway'的引用 设置bean属性'twNavigatorClientGateway';嵌套 异常是org.springframework.beans.factory.BeanCreationException: 创建名为'defaultTwNavigatorClientGateway'的bean时出错 在类路径资源[jkdsfj-spring.xml]中定义: 设置时无法解析对bean'twRestTemplate'的引用 bean属性'twRestTemplate';嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 在类路径中定义名为'twRestTemplate'的bean 资源[joggrouptwconnector-spring.xml]:无法解析引用 设置构造函数参数时bean'bufferingRequestFactory'; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 在类中定义名为'bufferingRequestFactory'的bean path resource [jkdsfj-spring.xml]:bean的实例化 失败;嵌套异常是java.lang.IllegalStateException:没有bean bean定义上指定的类
我哪里错了?
答案 0 :(得分:0)
Not sure what version of spring-web you are using. I've looked at spring-web.4.x. The BufferingClientHttpRequestFactory accepts a constructor argument. So, you can create one without a factory-bean using the constructor-arg
<bean id="bufferingRequestFactory" class="org.springframework.http.client.BufferingClientHttpRequestFactory"/>
<constructor-arg>
<ref bean="requestFactory" />
</constructor-arg>
</bean>
You may have to do the same for HttpCompnentsClientHttpRequestFactory.