我需要一个redisClient,我将其配置为Jedis pool.code:
@Configuration
public class RedisConfiguration {
@Bean(name= "jedis.pool")
@ConditionalOnMissingBean
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${jedis.pool.host}")String host,
@Value("${jedis.pool.port}")int port,@Value("${jedis.pool.timeout}")int timeout,@Value("${jedis.pool.password}")String password) {
return new JedisPool(config, host, port, timeout, password);
}
@Bean(name= "jedis.pool.config")
@ConditionalOnMissingBean
public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
@Value("${jedis.pool.config.maxIdle}")int maxIdle,
@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
}
RedisClient
这样的课程:
@Service
@EnableAutoConfiguration
public class RedisClient {
@Resource(name="jedis.pool")
private JedisPool jedisPool;
public void set(String key, String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
} finally {
if(jedis!=null)
jedis.close();
}
}
我像这样使用RedisClient
:
@Autowired
private RedisClient redisClient;
但是当代码运行时,使用redisClient会抛出NullPointerException
。