注入RestTemplate我有這部分代碼爲例:通過Spring
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);
我會做這樣的事情在春天。我寫了這個代碼:
<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>
,我把的ConnectionManager(帶連接設置),以及在closeableHttpClient超時管理的要求配置器(我看到這個鏈接)。我把這個bean放在HttpComponentClientHttpRequestFactory中,然後放在BufferingRequestFactory中,最後放在RestTemplate中。
但在運行時我得到這個錯誤:
WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultTwProductSearchService' defined in class path resource [jkdsfj-spring.xml]: Cannot resolve reference to bean 'twNavigatorClientGateway' while setting bean property 'twNavigatorClientGateway'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultTwNavigatorClientGateway' defined in class path resource [jkdsfj-spring.xml]: Cannot resolve reference to bean 'twRestTemplate' while setting bean property 'twRestTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'twRestTemplate' defined in class path resource [joggrouptwconnector-spring.xml]: Cannot resolve reference to bean 'bufferingRequestFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bufferingRequestFactory' defined in class path resource [jkdsfj-spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
我哪裏錯了?
可能是因爲'BufferingClientHttpRequestFactory'沒有實現'org.springframework.beans.factory.FactoryBean'? – Sashi
好的,我檢查。如果它沒有實現FactoryBean類,我該怎麼做? – sharkbait
是BufferingClientHttpRequestFactory沒有實現FactoryBean ... :(我現在不是什麼我必須做的 – sharkbait