Spring FactoryBean接口使用
2016-07-29 17:19
567 查看
简介
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>
相关文章推荐
- Spring(2)与接口配合使用和获取Bean容器的两种方式(BeanFactory 、ApplicationContext)
- Spring学习笔记 关于FactoryBean接口的使用
- 【第二章:源码解析】Spring的BeanFactory的接口注解
- 使用spring的ProxyFactoryBean来实现权限控制
- Spring的FactoryBean使用
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- Spring 如何将无法使用new操作符新建的对象作为spring的bean (使用FactoryBean接口)
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题
- Spring IOC ( 二) : FactoryBean接口(转载)
- 关于spring的BeanFactory在使用注解的注解事项
- 使用Spring的FactoryBean创建同步队列线程池
- Spring使用PropertyPathFactoryBean进行属性值注入
- Spring的接口BeanFactory和ApplicationContext
- Spring中AutowireCapableBeanFactory的使用
- 使用FactoryBean接口简化工厂Bean开发
- Spring-Bean的初始化(init方法和实现org.springframework.beans.factory.InitializingBean接口)
- 8.5.2: Spring的事务---使用TransactionProxyFactoryBean创建事务代理
- 自己动手写代码,整合Spring和Hibernate(一)和FactoryBean的使用
- 使用spring提供的LocalSessionFactoryBean来得到SessionFactory
- 使用FactoryBean接口实现自定义bean初始化