期望同一个 Service 中注入两个 RedisTemplate,redis1 用来操作 String,redis2 用来操作 Object 。如何实现?
@Resource
private RedisTemplate redis1;
@Resource
private RedisTemplate<String, Object> redis2;
1
Resource 2020-04-10 14:10:31 +08:00
|
2
WhereverYouGo OP @Resource [笑哭],这也行。你这表情咋发的 0.0
|
3
Resource 2020-04-10 14:16:06 +08:00
@sweetsorrow211 #2 Chrome 扩展:v2ex plus
|
4
HonoSV 2020-04-10 14:19:46 +08:00
哈哈哈哈 楼上什么鬼
|
5
wanacry 2020-04-10 14:20:43 +08:00
|
6
liqingcan 2020-04-10 14:25:06 +08:00
上下文存在 2 个 RedisTemplate,应该只能通过名字来注入了吧
|
7
chendy 2020-04-10 14:28:09 +08:00
用`@Qualifer`区分一下喽
或者构造方法注入 + bean 名字区分,无需注解…(需要 java8,需要编译参数 /spring 插件 |
8
WhereverYouGo OP |
9
WhereverYouGo OP |
10
EminemW 2020-04-10 15:10:44 +08:00 1
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.StringRedisTemplate; /** * Redis 配置 * */ @Slf4j @Configuration public class RedisConfig { /** * 配置 lettuce 连接池 * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig<>(); } /** * 配置数据源 * @return */ @Bean("redisConf") @Primary @ConfigurationProperties(prefix = "spring.redis") public RedisStandaloneConfiguration redisConfig() { return new RedisStandaloneConfiguration(); } @Bean("redis1Conf") @ConfigurationProperties(prefix = "spring.redis1") public RedisStandaloneConfiguration redisConfig1() { return new RedisStandaloneConfiguration(); } /** * 配置连接工厂 * @param poolConfig * @param redisConfig * @return */ @Bean("factory") @Primary public LettuceConnectionFactory factory(GenericObjectPoolConfig poolConfig,@Qualifier("redisConf") RedisStandaloneConfiguration redisConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } @Bean("factory1") public LettuceConnectionFactory factory1(GenericObjectPoolConfig poolConfig,@Qualifier("redis1Conf") RedisStandaloneConfiguration redisConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } /** * 默认 redis0 * @param connectionFactory * @return */ @Bean @Primary public StringRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory) { return getRedisTemplate(connectionFactory); } @Bean("redisTemplate1") public StringRedisTemplate redisTemplate1(@Qualifier("factory1")LettuceConnectionFactory factory) { return getRedisTemplate(factory); } @Bean @Primary public RedisUtil redis0Util(StringRedisTemplate redisTemplate) { return new RedisUtil(redisTemplate); } @Bean("redis1") public RedisUtil redis1Util(@Qualifier("redisTemplate1") StringRedisTemplate redisTemplate) { return new RedisUtil(redisTemplate); } private StringRedisTemplate getRedisTemplate(LettuceConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(factory); template.afterPropertiesSet(); return template; } } //注入的时候 @Autowired private RedisTemplate redis0; @Autowired @Qualifier("redis1") private RedisTemplate redis1; |
11
EminemW 2020-04-10 15:18:23 +08:00
我是自己封装了 RedisUtil 来用,
|
12
EminemW 2020-04-10 15:19:05 +08:00
|
13
WhereverYouGo OP @EminemW #12 老哥,稳....我研究下,感谢铁汁!
|
14
soulzz 2020-04-10 15:46:44 +08:00
|
15
Kyle18Tang 2020-04-10 16:13:38 +08:00
操作 String 默认不是有个 StringRedisTemplate 吗...自动配置里面本来就设置了 2 个 Template 的, 见 RedisAutoConfiguration 类.
|
16
liujan 2020-04-10 18:35:03 +08:00 via iPhone
顺便问下,对于那些没有实现序列化接口的类,有办法缓存到 redis 中吗
|
17
freezhan 2020-04-10 20:32:59 +08:00
|
18
1424659514 2020-04-11 09:18:58 +08:00
|
20
WhereverYouGo OP @liujan #19 用这个试试
@ Resource private RedisTemplate redisTemplate; ValueOperations<String, Object> operations = redisTemplate.opsForValue(); 我没 implements Serializable 也可以的 |
21
liujan 2020-04-13 12:03:39 +08:00 via iPhone
@sweetsorrow211 好,我试试,多谢
|