Spring FactoryBean接口使用

简介

Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的getObject方法所返回的对象。

一般情况下,Spring 通过反射机制利用 Bean 的 class 属性指定实现类实例化 Bean ,在某些情况下,实例化 Bean 过程比较复杂,如果按照传统的方式,则需要在Bean 中提供大量的配置信息。配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案。 Spring 为此提供了一个 org.springframework.bean.factory.FactoryBean 的工厂类接口,用户可以通过实现该接口定制实例化 Bean 的逻辑。

FactoryBean接口定义

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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值