RedisTemplate redisTemplate= new RedisTemplate<>(); 空指针异常
时间: 2024-06-13 09:09:10 浏览: 14
在这段代码中,RedisTemplate实例化后并没有进行任何配置,因此在调用redisTemplate.opsForHash()方法时会抛出空指针异常。正确的做法是在实例化RedisTemplate后进行相应的配置,例如设置连接工厂、序列化器等。下面是一个示例代码:
```java
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
```
相关问题
private static final Map<String, List<RedisTemplate>> VEST_REDISTEMPLATE_MAP = new ConcurrentHashMap<>(3); 只能放三个key吗
`ConcurrentHashMap` 的构造方法 `ConcurrentHashMap(int initialCapacity)` 中的 `initialCapacity` 参数表示初始容量,而不是容量上限。因此,`private static final Map<String, List<RedisTemplate>> VEST_REDISTEMPLATE_MAP = new ConcurrentHashMap<>(3);` 中的 `3` 只是表示初始容量为 `3`,并不表示 `ConcurrentHashMap` 的容量上限。如果需要限制 `Map` 的容量上限,可以考虑使用 `LinkedHashMap` 的构造方法 `LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)` 中的 `accessOrder` 参数,将其设置为 `true`,并实现 `removeEldestEntry` 方法,例如:
```java
private static final int MAX_CAPACITY = 3;
private static final Map<String, List<RedisTemplate>> VEST_REDISTEMPLATE_MAP = new LinkedHashMap<String, List<RedisTemplate>>(MAX_CAPACITY, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, List<RedisTemplate>> eldest) {
return size() > MAX_CAPACITY;
}
};
```
这样,当 `Map` 中的键值对数量超过 `MAX_CAPACITY` 时,就会将最老的键值对删除。
代码解释:@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); templa
这段代码是用于配置 Redis 的缓存,使用了 Spring 提供的缓存框架。具体解释如下:
- `@Configuration`:声明这个类是一个配置类,相当于传统的 XML 配置文件。
- `@EnableCaching`:启用缓存支持。
- `RedisConfig extends CachingConfigurerSupport`:继承了 `CachingConfigurerSupport` 类,这个类提供了一些缓存配置的默认实现,可以让我们重写需要的方法来实现自定义配置。
- `@Bean`:声明这个方法返回一个 Spring 管理的 Bean 对象。
- `RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory)`:定义了一个名为 `redisTemplate` 的 Bean,这个 Bean 的类型是 `RedisTemplate<String, Object>`,返回的是 RedisTemplate 对象。
- `RedisConnectionFactory factory`:声明了一个名为 `factory` 的参数,这个参数是 `RedisConnectionFactory` 类型,表示这个 Bean 需要一个 RedisConnectionFactory 对象作为参数。
- `RedisTemplate<String, Object> template = new RedisTemplate<>();`:创建一个 RedisTemplate 对象。
- `template.setConnectionFactory(factory)`:设置 RedisTemplate 对象的连接工厂,即上面传入的 RedisConnectionFactory 对象。
- `template.setValueSerializer(new GenericToStringSerializer<>(Object.class))`:设置 RedisTemplate 对象的值序列化器,这里使用了 `GenericToStringSerializer`,将任何类型的值转换为字符串后存储到 Redis 中。
- `return template`:返回 RedisTemplate 对象,这个对象将被 Spring 管理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)