赞
踩
Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的getObject方法所返回的对象。
一般情况下,Spring 通过反射机制利用 Bean 的 class 属性指定实现类实例化 Bean ,在某些情况下,实例化 Bean 过程比较复杂,如果按照传统的方式,则需要在Bean 中提供大量的配置信息。配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案。 Spring 为此提供了一个 org.springframework.bean.factory.FactoryBean 的工厂类接口,用户可以通过实现该接口定制实例化 Bean 的逻辑。
package org.springframework.beans.factory;
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
结合自己封装的一个http请求中间层来说明一下FactoryBean的使用。
GXHttpClient 在构造的时候需要接收一个 HttpClient httpclient的实例,HttpClient有3个子类:OkHttpClientImpl、AsyncHttpClientImpl、ApacheHttpClientImpl。
@ThreadSafe
public class GXHttpClient implements HttpRequest {
private HttpClient httpclient;
public GXHttpClient(HttpClient httpclient) {
this.httpclient = httpclient;
}
@Override
public String get(String url) throws HttpRequestException {
return httpclient.get(url);
}
}
我们使用FactoryBean来创建 HttpClient实例。代码如下:
/**
* ${DESCRIPTION}
*
* @author Ricky Fung
* @create 2016-09-01 10:12
*/
public class OkHttpClientFactory implements FactoryBean<HttpClient> {
private int readTimeout;
private int connectTimeout;
private int maxConnections;
private int maxConnectionPerHost;
private boolean requestRetryEnabled;
private String proxyHost;
private int proxyProt;
@Override
public HttpClient getObject() throws Exception {
HttpHost proxy = null;
if(StringUtils.isNotEmpty(proxyHost) && proxyProt>0){
proxy = new HttpHost(proxyHost, proxyProt);
}
HttpRequestConfig config = new HttpRequestConfig.Builder()
.setReadTimeout(readTimeout)
.setConnectTimeout(connectTimeout)
.setMaxConnections(maxConnections)
.setMaxConnectionPerHost(maxConnectionPerHost)
.requestRetryEnabled(requestRetryEnabled)
.setProxy(proxy)
.build();
return new OkHttpClientImpl(config);
}
@Override
public Class<?> getObjectType() {
return OkHttpClientImpl.class;
}
@Override
public boolean isSingleton() {
return true;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public int getMaxConnectionPerHost() {
return maxConnectionPerHost;
}
public void setMaxConnectionPerHost(int maxConnectionPerHost) {
this.maxConnectionPerHost = maxConnectionPerHost;
}
public boolean isRequestRetryEnabled() {
return requestRetryEnabled;
}
public void setRequestRetryEnabled(boolean requestRetryEnabled) {
this.requestRetryEnabled = requestRetryEnabled;
}
public String getProxyHost() {
return proxyHost;
}
public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
}
public int getProxyProt() {
return proxyProt;
}
public void setProxyProt(int proxyProt) {
this.proxyProt = proxyProt;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
<context:annotation-config />
<context:component-scan base-package="com.bytebeats.toolkit.samples" />
<bean id="gxhttpclient" class="com.bytebeats.toolkit.http.GXHttpClient">
<constructor-arg index="0">
<bean class="com.bytebeats.toolkit.samples.http.spring.OkHttpClientFactory">
<property name="readTimeout" value="2000"></property>
<property name="connectTimeout" value="2000"></property>
<property name="maxConnections" value="30"></property>
<property name="maxConnectionPerHost" value="15"></property>
<property name="requestRetryEnabled" value="false"></property>
<property name="proxyHost" value=""></property>
<property name="proxyProt" value="9100"></property>
</bean>
</constructor-arg>
</bean>
</beans>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。