首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何存储静态字段?

如何存储静态字段?
EN

Stack Overflow用户
提问于 2012-12-24 20:37:54
回答 1查看 345关注 0票数 0

在一个redis java客户端中,我发现了以下内容:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-24 20:40:51

你没有。

在spring中,更可取的做法是定义一个JedisPool bean,并在需要的地方自动连接它。

例如,使用xml配置:

代码语言:javascript
复制
 <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
      <constructor-arg>
           <bean class="redis.clients.jedis.JedisPoolConfig" />
      </consrtuctor-arg>
      <constructor-arg value="localhost" />
 </bean>

代码语言:javascript
复制
@Autowire
JedisPool jedisPool;

如果你使用spring java config,那就更简单了--你可以使用你发布的代码来定义pool bean:

代码语言:javascript
复制
@Configuration
public class Configuration {

    @Bean
    public JedisPool createJedisPool() { 
        return new JedisPool(new JedisPoolConfig(), "localhost");
    }
}

此外,您可能还想了解一下spring-data - redis

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14021521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档