当前位置:网站首页>@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/
边栏推荐
- CF1534F2-Falling Sand (Hard Version)
- ArcGIS Pro 创建tpk
- 如何利用原生JS实现回到顶部以及吸顶效果
- [21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
- 鲲鹏编译调试及原生开发工具基础知识
- How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
- 16. File upload
- Multilingual Translation - Multilingual Translation Software Free
- 给肯德基打工的调料商,年赚两亿
- 【mysql】mysql分别按年/月/日/周分组统计数据
猜你喜欢

sqlmap combined with dnslog fast injection

91.(cesium之家)cesium火箭发射模拟

Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?

In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?

web 性能提升(将持续更新……)

C language, operators of shift operators (> >, < <) explanation

Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)

从0开始设计JVM ,忘记名词跟上思路一次搞懂

15. 拦截器-HandlerInterceptor

Dump文件生成,内容,以及分析
随机推荐
SAS data processing technology (1)
Is there a way out in the testing industry if it is purely business testing?
16. 文件上传
The Missing Semester of Your CS Education
IEEE的论文哪里可以下载?
[数据可视化] 图表设计原则
Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)
Multilingual Translation - Multilingual Translation Software Free
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
Qt入门(六)——抽奖系统的实现
Easy-to-use translation plug-in - one-click automatic translation plug-in software
I caught a 10-year-old Ali test developer, and after talking about it, I made a lot of money...
两个链表的第一个公共节点——LeetCode
Software Testing Certificate (1) - Software Evaluator
2022.8.10-----leetcode.640
Special class and type conversion
Promote the high-quality development of denim clothing
5. Lombok
HGAME 2022 Week1 writeup
图片懒加载(纯手写)