spring框架中由FactoryBean获取JedisCluster实例
2017-05-14 00:50
441 查看
spring配置文件:
<bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisClusterFactory"> <property name="addressConfig" value="classpath:context-ela-static.properties"/> <property name="addressKeyPrefix" value="redis.cluster.address"/> <property name="timeout" value="${redis.cluster.timeout}"/> <property name="maxRedirections" value="${redis.cluster.maxRedirections}"/> <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig"/> </bean>
JedisClusterFactory.java文件
import com.google.common.collect.Maps; import com.google.common.primitives.Ints; import com.pingan.ela.structure.util.PropertiesUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.IPMaps; import redis.clients.jedis.JedisCluster; import java.io.IOException; import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; /** * Created by YANHAO123 on 2016/3/1. */ public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean { private Resource addressConfig; private String addressKeyPrefix ; private JedisCluster jedisCluster; private Integer timeout; private Integer maxRedirections; private GenericObjectPoolConfig genericObjectPoolConfig; @Override public JedisCluster getObject() throws Exception { return jedisCluster; } @Override public Class<? extends JedisCluster> getObjectType() { return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class); } @Override public boolean isSingleton() { return true; } private Set<HostAndPort> parseHostAndPort() throws IOException { Properties prop = new Properties(); prop.load(addressConfig.getInputStream()); // 增加非空判断,忽略值为空的配置 return Maps.filterKeys(prop, k -> ((String) k).startsWith(addressKeyPrefix)).values().stream().map(v -> (String) v).filter(StringUtils::isNotBlank).map(v -> v.split(":")).map(s -> new HostAndPort(s[0], Ints.tryParse(s[1]), s[2])).collect(Collectors.toSet()); } @Override public void afterPropertiesSet() throws Exception { Set<HostAndPort> haps = this.parseHostAndPort(); jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig); } public void setAddressConfig(Resource addressConfig) { this.addressConfig = addressConfig; } public void setTimeout(int timeout) { this.timeout = timeout; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public void setAddressKeyPrefix(String addressKeyPrefix) { this.addressKeyPrefix = addressKeyPrefix; } public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) { String[] oldIP = PropertiesUtils.getInstance().getProperty("redis.cluster.setIPMap.IP").split("[|]"); String[] newIP = PropertiesUtils.getInstance().getProperty("redis.cluster.setIPMap.natIP").split("[|]"); for(int i=0;i<oldIP.length;i++){ IPMaps.setIPMap(oldIP[i], newIP[i]); } this.genericObjectPoolConfig = genericObjectPoolConfig; } }
相关文章推荐
- 基于spring框架,以bean形式配置实例,用于获取oracle上已存在sequence的递增值。
- Spring中通过实现FactoryBean接口获取bean实例
- Spring学习之使用factorybean获取bean实例
- spring获取类型为FactoryBean的factory实例和其生产的实例
- 获取spring bean 容器中实例,非创建新实例
- spring获取bean 实例
- Spring获取bean方式(BeanFactory和ApplicationContext)
- 通过BeanFactory获取bean的几种方法
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
- spring获取bean 实例
- java 高新技术【11.2】 动态代理类 编写类似 AOP 与 BeanFactory。初试Spring框架
- java 高新技术【11.2】 动态代理类 编写类似 AOP 与 BeanFactory。初试Spring框架
- spring框架下获取bean的方式总结(转)
- 在Spring框架下获取Bean的方式总结(转)
- 在Spring框架下获取Bean的方式总结(原创)
- 在Spring框架下获取Bean的方式总结
- Spring框架下获取Bean的几种方式
- Spring框架下获取Bean的几种方式
- Bean&BeanFactory&创建bean实例
- 获取配置文件中的Bean实例