1 配置文件添加路径
#redis地址
redis.host=xx.xxx.xx.xx
#端口
redis.port=xxxx
#连接超时时间
redis.timeout=3000
#密码
redis.password=xxxxxx
2 我这里选择配置文件注入
<bean id="JedisPool" class="com.ruixiude.trip.redis.JedisPoolUtils">
<constructor-arg index="0" value="${redis.host}" />
<constructor-arg index="1" value="${redis.port}" />
<constructor-arg index="2" value="${redis.timeout}" />
<constructor-arg index="3" value="${redis.password}" />
</bean>
3工具类封装连接池
public class JedisPoolUtils {
private static JedisPool jedisPool = null;
@Value("${redis.host}")
protected String host;
@Value("${redis.port}")
protected int port;
@Value("${redis.timeout}")
protected int timeout;
@Value("${redis.password}")
protected String password;
public JedisPoolUtils(String host, int port, int timeout, String password) {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
jedisPool = new JedisPool(poolConfig,host,port,timeout,password);
}
// static {
// GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
// jedisPool = new JedisPool(poolConfig,host,PORT,TIMEOUT,password);
// }
public static Jedis getResource() {
return jedisPool.getResource();
}
}
4 就可以使用了
Jedis jedis = JedisPoolUtils.getResource(); //在连接池中获取jedis连接对象
String hget = jedis.hget("主题", key); //Redis获取数据
EngineView view = JSON.parseObject(hget, EngineView.class);