Java 如何 - Spring IoC 和 HttpClient 4.3.1 CloseableHttpClient?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20340470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to - Spring IoC and HttpClient 4.3.1 CloseableHttpClient?
提问by nsayer
I'd like to have Spring IoC configure a CloseableHttpClient
object and inject it into my class so that customization of its configuration can be done via XML.
我想让 Spring IoC 配置一个CloseableHttpClient
对象并将其注入我的类,以便可以通过 XML 完成其配置的自定义。
From what I can see, HttpClient
seems to resist this pattern quite forcibly. They want you to do things like
就我所见,HttpClient
似乎相当强硬地抵制这种模式。他们希望你做这样的事情
CloseableHttpClient chc =
HttpClients.custom().set<thing that should be a property>().build();
Ick.
哎呀。
Is there not some mechanism for making a singleton CloseableHttpClient
bean that I can then use?
没有一些机制可以制作一个CloseableHttpClient
我可以使用的单例bean 吗?
采纳答案by Matt
This seems to work for me:
这似乎对我有用:
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
factory-method="custom">
<property name="socketTimeout" value="${socketTimeoutInMillis}" />
<property name="connectTimeout" value="${connectionTimeoutInMillis}" />
</bean>
<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="defaultRequestConfig" ref="requestConfig" />
</bean>
<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />
That gives me a CloseableHttpClient in the "httpClient" bean, with the socket and connection timeouts configured. You should be able to add more properties to either the requestConfigBuilder or the httpClientBuilder.
这在“httpClient”bean 中为我提供了一个 CloseableHttpClient,并配置了套接字和连接超时。您应该能够向 requestConfigBuilder 或 httpClientBuilder 添加更多属性。
回答by Sotirios Delimanolis
With Java config, this is as simple as
使用 Java 配置,这很简单
@Bean
public CloseableHttpClient httpClient() {
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setEverything(everything); // configure it
CloseableHttpClient httpClient = builder.build();
}
With XML config, it's a little more complex. You can create your own FactoryBean
implementation, say CloseableHttpClientFactoryBean
, which delegates all the calls to a HttpClientBuilder
and calls build()
inside getObject()
.
使用 XML 配置,它有点复杂。例如,您可以创建自己的FactoryBean
实现,CloseableHttpClientFactoryBean
它将所有调用委托给 aHttpClientBuilder
并调用build()
inside getObject()
。
public class CloseableHttpClientFactoryBean implements FactoryBean<CloseableHttpClient> {
private HttpClientBuilder builder;
public CloseableHttpClientFactoryBean() {
builder = HttpClientBuilder.create();
}
... // all the setters
// for example
public void setEverything(Everything everything) {
// delegate
builder.setEverything(everything);
}
public CloseableHttpClient getObject() {
return builder.build();
}
}
And the config
和配置
<bean name="httpClient" class="com.spring.http.clients.CloseableHttpClientFactoryBean">
<property name="everything" ref="everything"/>
</bean>
You will need a setter method for each HttpClientBuilder
method.
每种HttpClientBuilder
方法都需要一个 setter方法。
Note that if you don't need any custom configuration, you can use factory-method
to get a default CloseableHttpClient
请注意,如果您不需要任何自定义配置,则可以使用factory-method
获取默认值CloseableHttpClient
<bean name="httpClient" class="org.apache.http.impl.client.HttpClients" factory-method="createDefault" >
</bean>