我使用ektorp连接到CouchDB。
构建ektorp HttpClient
实例的方法是使用构建器模式:
HttpClient httpClient = new StdHttpClient.Builder()
.host("mychouchdbhost")
.port(4455)
.build();
我对Spring比较陌生。请告诉我如何在我的上下文中配置HttpClient
以通过Builder
创建它。
执行此操作的一种方法是使用@Configuration
。还有其他选择吗?
答案 0 :(得分:46)
您可以尝试实施FactoryBean
界面:
public class HttpFactoryBean implements FactoryBean<HttpClient>{
private String host;
private int port;
public HttpClient getObject() throws Exception {
return new StdHttpClient.Builder()
.host(host)
.port(port)
.build();
}
public Class<? extends HttpClient> getObjectType() {
return StdHttpClient.class;
}
public boolean isSingleton() {
return true;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}}
并在bean定义后添加配置:
<beans ...">
<bean name="myHttpClient" class="HttpFactoryBean">
<property name="port" value="8080"/>
<property name="host" value="localhost"/>
</bean>
</beans>
然后你可以将这个bean注入另一个bean,它将被解析为StdHttpClient
实例。
答案 1 :(得分:6)
当我开发flexy-pool (a reactive connection pool sizing utility)时,我偶然发现了同样的问题,因此我用基于Java和基于xml的示例编写了an article。
基本上,从以下构建器开始:
public final class Configuration<T extends DataSource> extends ConfigurationProperties<T, Metrics, PoolAdapter<T>> {
public static final long DEFAULT_METRIC_LOG_REPORTER_PERIOD = 5;
public static class Builder<T extends DataSource> {
private final String uniqueName;
private final T targetDataSource;
private final PoolAdapterBuilder<T> poolAdapterBuilder;
private final MetricsBuilder metricsBuilder;
private boolean jmxEnabled = true;
private long metricLogReporterPeriod = DEFAULT_METRIC_LOG_REPORTER_PERIOD;
public Builder(String uniqueName, T targetDataSource, MetricsBuilder metricsBuilder, PoolAdapterBuilder<T> poolAdapterBuilder) {
this.uniqueName = uniqueName;
this.targetDataSource = targetDataSource;
this.metricsBuilder = metricsBuilder;
this.poolAdapterBuilder = poolAdapterBuilder;
}
public Builder setJmxEnabled(boolean enableJmx) {
this.jmxEnabled = enableJmx;
return this;
}
public Builder setMetricLogReporterPeriod(long metricLogReporterPeriod) {
this.metricLogReporterPeriod = metricLogReporterPeriod;
return this;
}
public Configuration<T> build() {
Configuration<T> configuration = new Configuration<T>(uniqueName, targetDataSource);
configuration.setJmxEnabled(jmxEnabled);
configuration.setMetricLogReporterPeriod(metricLogReporterPeriod);
configuration.metrics = metricsBuilder.build(configuration);
configuration.poolAdapter = poolAdapterBuilder.build(configuration);
return configuration;
}
}
private final T targetDataSource;
private Metrics metrics;
private PoolAdapter poolAdapter;
private Configuration(String uniqueName, T targetDataSource) {
super(uniqueName);
this.targetDataSource = targetDataSource;
}
public T getTargetDataSource() {
return targetDataSource;
}
public Metrics getMetrics() {
return metrics;
}
public PoolAdapter<T> getPoolAdapter() {
return poolAdapter;
}
}
使用基于Java的配置很简单:
@org.springframework.context.annotation.Configuration
public class FlexyDataSourceConfiguration {
@Bean
public Configuration configuration() {
return new Configuration.Builder(
UUID.randomUUID().toString(),
poolingDataSource,
CodahaleMetrics.BUILDER,
BitronixPoolAdapter.BUILDER
).build();
}
}
但您也可以使用基于XML的配置:
<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder">
<constructor-arg value="uniqueId"/>
<constructor-arg ref="poolingDataSource"/>
<constructor-arg value="#{ T(com.vladmihalcea.flexypool.metric.codahale.CodahaleMetrics).BUILDER }"/>
<constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.BitronixPoolAdapter).BUILDER }"/>
</bean>
<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/>
答案 2 :(得分:3)
请查看Spring FactoryBean和FactoryMethod文档。
答案 3 :(得分:1)
虽然不明确你的情况;如果它通过标准bean模式set
方法公开属性,则可以扩展构建器。例如,如果我们以org.apache.httpcomponents:httpclient
HttpClientBuilder
为例,我们可以提供以下内容:
public class HttpClientFactoryBean
extends HttpClientBuilder
implements InitializingBean,
FactoryBean<HttpClient> {
private HttpClient value;
@Override
public void afterPropertiesSet() throws Exception {
this.value = build();
}
@Override
public HttpClient getObject() throws Exception {
return value;
}
@Override
public Class<?> getObjectType() {
return HttpClient.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
现在,工厂bean可以访问HttpClientBuilder
公开的任何方法。现在可以进行如下配置:
<beans id="httpClient" class="com.drunkendev.factory.HttpClientFactoryBean">
<beans name="defaultCredentialsProvider" ref="credentialsProvider"/>
<beans name="targetAuthenticationStrategy">
<util:constant static-field="org.apache.http.impl.client.TargetAuthenticationStrategy.INSTANCE"/>
</beans>
</beans>
答案 4 :(得分:0)
虽然FactoryBean
更干净,但使用SpEL可以使污垢更快速。
这就是我刚刚配置Neo4j driver的方式:
<bean id = "neoDriver" class = "org.neo4j.driver.v1.GraphDatabase"
factory-method="driver">
<constructor-arg value = "bolt://127.0.0.1:7687" />
<constructor-arg>
<bean class = "org.neo4j.driver.v1.AuthTokens" factory-method = "basic">
<constructor-arg value = "neo4j" />
<constructor-arg value = "***" />
</bean>
</constructor-arg>
<constructor-arg type="org.neo4j.driver.v1.Config"
value = "#{T(org.neo4j.driver.v1.Config).build ()
.withConnectionAcquisitionTimeout ( 10, T(java.util.concurrent.TimeUnit).SECONDS )
.withConnectionTimeout ( 10, T(java.util.concurrent.TimeUnit).SECONDS )
.toConfig ()
}"
/>
</bean>
从工厂方法的第3个参数可以看到,可以将生成器及其方法作为SpEL表达式来调用,但要注意的是,必须通过其FQN指定类。但这避免了您编写整个样板FactoryBean。