当前位置:网站首页>@Autowired注入RedisCache报错空指针
@Autowired注入RedisCache报错空指针
2022-08-11 00:07:00 【穆雄雄】
大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂
今天在改一个几年前写的项目中的代码,其中有个地方用到了缓存,不过代码中用的是:CacheUtils
,也不是报错,但是就是在并发的时候有问题,所以就想着换换。
于是就换了个Redis
的工具类,工具类代码如下:
package com.bltsc.common.untils.cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/** * @author: muxiongxiong * @date: 2022年08月10日 上午 11:45 * 公众号:雄雄的小课堂 * 博客:https://blog.csdn.net/qq_34137397 * 个人站:http://www.穆雄雄.com * 个人站:http://www.muxiongxiong.cn * @Description: 类的描述 */
@SuppressWarnings(value = {
"unchecked", "rawtypes" })
@Component
public class RedisCache {
@Autowired
public RedisTemplate redisTemplate;
/** * 缓存基本的对象,Integer、String、实体类等 * * @param key 缓存的键值 * @param value 缓存的值 */
public <T> void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
/** * 缓存基本的对象,Integer、String、实体类等 * * @param key 缓存的键值 * @param value 缓存的值 * @param timeout 时间 * @param timeUnit 时间颗粒度 */
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/** * 设置有效时间 * * @param key Redis键 * @param timeout 超时时间 * @return true=设置成功;false=设置失败 */
public boolean expire(final String key, final long timeout)
{
return expire(key, timeout, TimeUnit.SECONDS);
}
/** * 设置有效时间 * * @param key Redis键 * @param timeout 超时时间 * @param unit 时间单位 * @return true=设置成功;false=设置失败 */
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
/** * 获得缓存的基本对象。 * * @param key 缓存键值 * @return 缓存键值对应的数据 */
public <T> T getCacheObject(final String key)
{
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/** * 删除单个对象 * * @param key */
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
/** * 删除集合对象 * * @param collection 多个对象 * @return */
public long deleteObject(final Collection collection)
{
return redisTemplate.delete(collection);
}
/** * 缓存List数据 * * @param key 缓存的键值 * @param dataList 待缓存的List数据 * @return 缓存的对象 */
public <T> long setCacheList(final String key, final List<T> dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/** * 获得缓存的list对象 * * @param key 缓存的键值 * @return 缓存键值对应的数据 */
public <T> List<T> getCacheList(final String key)
{
return redisTemplate.opsForList().range(key, 0, -1);
}
/** * 缓存Set * * @param key 缓存键值 * @param dataSet 缓存的数据 * @return 缓存数据的对象 */
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
/** * 获得缓存的set * * @param key * @return */
public <T> Set<T> getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
/** * 缓存Map * * @param key * @param dataMap */
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/** * 获得缓存的Map * * @param key * @return */
public <T> Map<String, T> getCacheMap(final String key)
{
return redisTemplate.opsForHash().entries(key);
}
/** * 往Hash中存入数据 * * @param key Redis键 * @param hKey Hash键 * @param value 值 */
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{
redisTemplate.opsForHash().put(key, hKey, value);
}
/** * 获取Hash中的数据 * * @param key Redis键 * @param hKey Hash键 * @return Hash中的对象 */
public <T> T getCacheMapValue(final String key, final String hKey)
{
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/** * 获取多个Hash中的数据 * * @param key Redis键 * @param hKeys Hash键集合 * @return Hash对象集合 */
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/** * 获得缓存的基本对象列表 * * @param pattern 字符串前缀 * @return 对象列表 */
public Collection<String> keys(final String pattern)
{
return redisTemplate.keys(pattern);
}
}
但是我在使用的时候:
@Autowired
private RedisCache redisCache;
会报空指针异常,于是经过网上搜罗,解决办法如下:
新建一个手动注入类:
package com.bltsc.moudles.common.until;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/** * @author: muxiongxiong * @date: 2022年08月10日 下午 3:27 * 公众号:雄雄的小课堂 * 博客:https://blog.csdn.net/qq_34137397 * 个人站:http://www.穆雄雄.com * 个人站:http://www.muxiongxiong.cn * @Description: 类的描述 */
@Component
public class BeanUtils implements ApplicationContextAware {
protected static ApplicationContext applicationContext ;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static Object getBean(String name) {
//name表示其他要注入的注解name名
return applicationContext.getBean(name);
}
/** * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象 */
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
然后在使用的时候,如下:
private RedisCache redisCache = BeanUtils.getBean(RedisCache.class);
这样注入即可。
参考文章:https://blog.csdn.net/wcy18818429914/article/details/106858974/
边栏推荐
- ROS Experimental Notes - Install QPEP and Intel-MKL
- 李彦宏拆墙交朋友,大厂“塑料友情”能否帮百度啃下硬骨头?
- 16. 文件上传
- How to do patent mining, the key is to find patent points, in fact, it is not too difficult
- 13. 内容协商
- Part of the reserve bank is out of date
- 英文文献阅读时,如何做笔记?
- [Excel知识技能] 将数值格式数字转换为文本格式
- C language, operators of shift operators (> >, < <) explanation
- ASIO4ALL是什么
猜你喜欢
给肯德基打工的调料商,年赚两亿
“蔚来杯“2022牛客暑期多校训练营2 DGHJKL题解
Ali P7 bask in January payroll: hard to fill the, really sweet...
Why do programming languages have the concept of variable types?
16. File upload
16. 文件上传
IEEE的论文哪里可以下载?
有哪些可以投稿软件工程/系统软件/程序设计语言类外文期刊、会议?
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
7. yaml
随机推荐
16. 文件上传
ADC和DAC记录
[Excel knowledge and skills] Convert text numbers to numeric format
9. Rest style request processing
Web-based meal ordering system in epidemic quarantine area
10. Notes on receiving parameters
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
15. 拦截器-HandlerInterceptor
“蔚来杯“2022牛客暑期多校训练营4 ADHK题解
2. Dependency management and automatic configuration
CF1427F-Boring Card Game【贪心】
[Excel知识技能] 将“假“日期转为“真“日期格式
C language% (%d,%c...)
ROS Experiment Notes - Validation of UZH-FPV Dataset
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
从0开始设计JVM ,忘记名词跟上思路一次搞懂
10. 接收参数相关注解
鲲鹏编译调试及原生开发工具基础知识
15. Interceptor - HandlerInterceptor
[C language] Implementation of guessing number game