Created
June 29, 2013 14:54
-
-
Save HeartSaVioR/5891411 to your computer and use it in GitHub Desktop.
JedisPool 의 생성자는 host, port 입력까지는 Config 객체를 받지 않다가, timeout, password, db number 등을 추가로 입력할 때에는 Config 객체를 명시적으로 입력받습니다.
Spring 을 통해 JedisPool bean 을 생성할 때, 이런 생성자 전달인자 차이는 bean 생성에 불편하게 작용합니다.
이를 피하기 위해, FactoryBean 을 정의하고 bean 에 설정되는 정보에 맞게 JedisPool 객체를 만들도록 구현하였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.heartsavior.spring.jedis; | |
import org.apache.commons.pool.impl.GenericObjectPool.Config; | |
import org.springframework.beans.factory.config.AbstractFactoryBean; | |
import redis.clients.jedis.JedisPool; | |
import redis.clients.jedis.Protocol; | |
public class JedisPoolFactoryBean extends AbstractFactoryBean<JedisPool> { | |
private String host; | |
private Integer port; | |
private Integer timeout; | |
private String password; | |
private Integer dbNumber; | |
@Override | |
protected JedisPool createInstance() throws Exception { | |
if (host == null) | |
return null; | |
return new JedisPool(new Config(), host, | |
port != null ? port : Protocol.DEFAULT_PORT, | |
timeout != null ? timeout : Protocol.DEFAULT_TIMEOUT, | |
password, dbNumber != null ? dbNumber : Protocol.DEFAULT_DATABASE); | |
} | |
@Override | |
public Class<?> getObjectType() { | |
return JedisPool.class; | |
} | |
public String getHost() { | |
return host; | |
} | |
public void setHost(String host) { | |
this.host = host; | |
} | |
public Integer getPort() { | |
return port; | |
} | |
public void setPort(Integer port) { | |
this.port = port; | |
} | |
public Integer getTimeout() { | |
return timeout; | |
} | |
public void setTimeout(Integer timeout) { | |
this.timeout = timeout; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public Integer getDbNumber() { | |
return dbNumber; | |
} | |
public void setDbNumber(Integer dbNumber) { | |
this.dbNumber = dbNumber; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment