|
| 1 | +package me.zuhr.demo.redis.config; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 4 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import me.zuhr.demo.redis.utils.RedisUtils; |
| 7 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 10 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.context.annotation.PropertySource; |
| 14 | +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
| 15 | +import org.springframework.data.redis.core.RedisTemplate; |
| 16 | +import org.springframework.data.redis.core.StringRedisTemplate; |
| 17 | +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
| 18 | +import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author zurun |
| 22 | + * @date 2018/2/9 17:51:33 |
| 23 | + */ |
| 24 | +//@Configuration |
| 25 | +//@ConfigurationProperties(prefix = "spring.redis") |
| 26 | +//@PropertySource("classpath:redis.properties") |
| 27 | +//@EnableCaching |
| 28 | +@EnableConfigurationProperties |
| 29 | +@Configuration |
| 30 | +@PropertySource(value = "classpath:/config/redis.properties") |
| 31 | +//@ConfigurationProperties(prefix = "spring.redis") |
| 32 | +public class RedisConfig { |
| 33 | + @Value("${spring.redis.host}") |
| 34 | + private String host; |
| 35 | + @Value("${spring.redis.port}") |
| 36 | + private int port; |
| 37 | + @Value("${spring.redis.password}") |
| 38 | + private String password; |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * 连接redis的工厂类 |
| 43 | + * |
| 44 | + * @return |
| 45 | + */ |
| 46 | + @Bean |
| 47 | + public JedisConnectionFactory jedisConnectionFactory() { |
| 48 | + JedisConnectionFactory factory = new JedisConnectionFactory(); |
| 49 | + factory.setHostName(host); |
| 50 | + factory.setPort(port); |
| 51 | +// factory.setTimeout(timeout); |
| 52 | + factory.setPassword(password); |
| 53 | +// factory.setDatabase(database); |
| 54 | + return factory; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 配置RedisTemplate |
| 59 | + * 设置添加序列化器 |
| 60 | + * key 使用string序列化器 |
| 61 | + * value 使用Json序列化器 |
| 62 | + * //TODO-zurun 还有一种简答的设置方式,改变defaultSerializer对象的实现。 |
| 63 | + * |
| 64 | + * @return |
| 65 | + */ |
| 66 | +// @Bean |
| 67 | +// public RedisTemplate<String, String> redisTemplate() { |
| 68 | +// //StringRedisTemplate的构造方法中默认设置了stringSerializer |
| 69 | +// RedisTemplate<String, String> template = new RedisTemplate<>(); |
| 70 | +// //set key serializer |
| 71 | +// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
| 72 | +// template.setKeySerializer(stringRedisSerializer); |
| 73 | +// template.setHashKeySerializer(stringRedisSerializer); |
| 74 | +// |
| 75 | +// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
| 76 | +// ObjectMapper objectMapper = new ObjectMapper(); |
| 77 | +// objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| 78 | +// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| 79 | +// |
| 80 | +// jackson2JsonRedisSerializer.setObjectMapper(objectMapper); |
| 81 | +// //set value serializer |
| 82 | +// template.setDefaultSerializer(jackson2JsonRedisSerializer); |
| 83 | +// |
| 84 | +// template.setConnectionFactory(jedisConnectionFactory()); |
| 85 | +// template.afterPropertiesSet(); |
| 86 | +// return template; |
| 87 | +// } |
| 88 | + @Bean |
| 89 | + RedisTemplate<String, Object> objRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory) { |
| 90 | + RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); |
| 91 | + redisTemplate.setConnectionFactory(connectionFactory); |
| 92 | + redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class)); |
| 93 | + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
| 94 | + redisTemplate.setKeySerializer(stringRedisSerializer); |
| 95 | +// redisTemplate.setValueSerializer(stringRedisSerializer); |
| 96 | + redisTemplate.setHashKeySerializer(stringRedisSerializer); |
| 97 | + return redisTemplate; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /** |
| 102 | + * 注:Qualifier注解用来指定bean,消除重复bean提示 |
| 103 | + * |
| 104 | + * @param connectionFactory |
| 105 | + * @return |
| 106 | + */ |
| 107 | + @Bean |
| 108 | + public RedisUtils redisUtils(@Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory) { |
| 109 | + return new RedisUtils(connectionFactory); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public String getHost() { |
| 114 | + return host; |
| 115 | + } |
| 116 | + |
| 117 | + public void setHost(String host) { |
| 118 | + this.host = host; |
| 119 | + } |
| 120 | + |
| 121 | + public int getPort() { |
| 122 | + return port; |
| 123 | + } |
| 124 | + |
| 125 | + public void setPort(int port) { |
| 126 | + this.port = port; |
| 127 | + } |
| 128 | + |
| 129 | + public String getPassword() { |
| 130 | + return password; |
| 131 | + } |
| 132 | + |
| 133 | + public void setPassword(String password) { |
| 134 | + this.password = password; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments