2012-12-24 68 views
0

在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. 

我只是想知道,春暖花開,我怎麼可以存儲靜態JedisPool。

回答

1

你不知道。

在春天,最好定義一個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> 

,然後,你的bean中:

@Autowire 
JedisPool jedisPool; 

如果使用spring java config這是更簡單 - 您可以使用您所發佈定義代碼池豆:

@Configuration 
public class Configuration { 

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

你也可以看看spring-data - redis