直接上代碼
package cc.openwiki.login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@Configuration
public class RedisConfiguration {
private RelaxedPropertyResolver propertyResolver;
@Autowired
public void setEnvironment(Environment env) {
this.propertyResolver = new RelaxedPropertyResolver(env, "spring.redis.");
}
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setPort(Integer.valueOf(propertyResolver.getProperty("port")));
factory.setHostName(propertyResolver.getProperty("host"));
factory.setTimeout(Integer.valueOf(propertyResolver.getProperty("timeout")));
return factory;
}
@Bean
public StringRedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
return stringRedisTemplate;
}
@Bean
public HashOperations<String, String, String> hashOperations(StringRedisTemplate redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(StringRedisTemplate redisTemplate) {
return redisTemplate.opsForValue();
}
}