Spring:使用构build器模式创build一个bean

我使用ektorp连接到CouchDB。

构buildektorp HttpClient实例的方法是使用构build器模式:

 HttpClient httpClient = new StdHttpClient.Builder() .host("mychouchdbhost") .port(4455) .build(); 

我对Spring比较陌生。 请咨询我如何在我的上下文中configuration一个HttpClient来通过Builder创build它。

一种方法是使用@Configuration 。 还有其他的select吗?

你可以尝试实现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; }} 

并添加到configuration下面的bean定义:

 <beans ..."> <bean name="myHttpClient" class="HttpFactoryBean"> <property name="port" value="8080"/> <property name="host" value="localhost"/> </bean> </beans> 

然后你可以将这个bean注入到另一个bean,它将被parsing为StdHttpClient实例。

当我开发flexy-pool(一个被动的连接池大小调整工具)时 ,我曾经偶然发现过同样的问题,于是我写了一篇基于Java和xml 的文章 。

基本上,从以下build造者开始:

 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的configuration非常简单:

 @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的configuration:

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

请检查Spring FactoryBean和FactoryMethod文档。

虽然不明确的情况下, 如果通过标准的bean模式set方法公开属性,就可以扩展一个构build器。 即如果我们采取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; } } 

现在任何由HttpClientBuilder公开的方法都可以被你的工厂bean访问。 现在可以使用以下configuration:

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