当前位置:   article > 正文

Spring FactoryBean接口使用_factorybean中getobjecttype

factorybean中getobjecttype

简介

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

示例

结合自己封装的一个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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

我们使用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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1018793
推荐阅读
相关标签
  

闽ICP备14008679号