0
votes

I have this part of code as example:

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

I would do something like this in Spring. I wrote this code:

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

where I put a connectionManager (with connection settings) and a request configurator for the timeout management in a closeableHttpClient (I saw this link). The I put this bean in a HttpComponentClientHttpRequestFactory, then in a BufferingRequestFactory and finally in the RestTemplate.

But at runtime I obtain this error:

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

Where I'm wrong?

1
May be because 'BufferingClientHttpRequestFactory' does not implement 'org.springframework.beans.factory.FactoryBean' ? - Sashi
ok I check. If it doesn't implement the FactoryBean class, how I have to do???? - sharkbait
Yes BufferingClientHttpRequestFactory doesn't implement FactoryBean... :( I don't now what I have to do - sharkbait

1 Answers

0
votes

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.