I use ektorp to connect to CouchDB.
The way to build an ektorp HttpClient
instance is to use builder pattern:
HttpClient httpClient = new StdHttpClient.Builder()
.host("mychouchdbhost")
.port(4455)
.build();
I am relatively new to Spring. Please advice me on how I can configure an HttpClient
in my context to create it via the Builder
.
One way to do this is with @Configuration
. Are any other options?
While not explicit for your case; it is possible to extend a builder if it exposes properties via standard bean pattern
set
methods. i.e. if we take theorg.apache.httpcomponents:httpclient
HttpClientBuilder
as an example we could have the following:Now any method exposed by
HttpClientBuilder
is accessible to your factory bean. A configuration such as the following is now possible:Please check Spring FactoryBean and FactoryMethod documentation.
You may try to implement
FactoryBean
interface:And add to config following bean definition:
Then you can inject this bean to another beans, it will be resolved as
StdHttpClient
instance.I once stumbled on the same issue, when I was developing flexy-pool (a reactive connection pool sizing utility), so I wrote an article with both a Java-based and an xml-based example.
Basically, starting from the following Builder:
Using the Java-based configuration is straight-forward:
But you can also use XML-based configuration as well: