在一个redis java客户端中,我发现了以下内容:
To use it, init a pool:
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
You can store the pool somewhere statically, it is thread-safe.我只是想知道,使用spring,我如何静态地存储JedisPool。
发布于 2012-12-24 20:40:51
你没有。
在spring中,更可取的做法是定义一个JedisPool bean,并在需要的地方自动连接它。
例如,使用xml配置:
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg>
<bean class="redis.clients.jedis.JedisPoolConfig" />
</consrtuctor-arg>
<constructor-arg value="localhost" />
</bean>@Autowire
JedisPool jedisPool;如果你使用spring java config,那就更简单了--你可以使用你发布的代码来定义pool bean:
@Configuration
public class Configuration {
@Bean
public JedisPool createJedisPool() {
return new JedisPool(new JedisPoolConfig(), "localhost");
}
}此外,您可能还想了解一下spring-data - redis
https://stackoverflow.com/questions/14021521
复制相似问题