问题描述

今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, obj)方法一直报 java.lang.nullpointerexception 异常,经过调试发现template为null。

Spring 注入失败

可能的原因: 网上百度了很久,原因可能在于我的utils包的类和controller的类不是同一个上下文。

解决办法

通过添加以下三个关键的地方,可以解决该问题

关于@PostConstruct:被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
//解决工具类中注入 3个步骤
@Component //关键步骤1,添加Component
public class RedisUtils { @Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, Object> template; public static RedisUtils redisUtils; // 关键2 添加该类的静态对象
protected static Logger logger = LoggerFactory.getLogger(RedisUtils.class); public RedisUtils() {
} // 关键3 用PostConstruct修饰init方法,并在init方法中对其赋值
@PostConstruct
public void init() {
redisUtils = this;
redisUtils.template = this.template;
redisUtils.stringRedisTemplate = this.stringRedisTemplate;
}

这样就可以通过redisUtils.template.opsForValue().set(key, obj)调用了

Spring @Autowired注解在非Controller中注入为null的更多相关文章

  1. Spring boot @Autowired注解在非Controller中注入为null

    参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777

  2. Spring @Autowired注解在非Controller/Service中注入为null

    参考:https://blog.csdn.net/qq_35056292/article/details/78430777 问题出现: 在一个非controller/service类中,我需要注入Co ...

  3. 【转】在SpringMVC Controller中注入Request成员域

    原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...

  4. 在SpringMVC Controller中注入Request成员域

    主题 在工作中遇到1个问题....我们定义了一个Controller基类,所有Springmvc自定义的controller都继承它....在它内部定义一个@Autowired HttpServlet ...

  5. SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别

    先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...

  6. spring aop在mvc的controller中加入切面无效

    spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...

  7. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  8. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

  9. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

随机推荐

  1. [ Codeforces Round #554 (Div. 2) C]

    C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. 使用Visual Studio 2017 C++17模块(module)特性

    环境: win7_x64旗舰版.VS2017企业版 一.安装VS2017 1.1 安装VS2017时,必须要勾选“使用C++的modules开发”选项 1.2 安装成功后,会在VS安装目录Micros ...

  3. D# 语法

    这篇文章 随意 的 记录 关于 D# 语法的想法 . template 和 interface   同时作为一等公民 D# 是程序员的语言,不需要太多包装和修饰, D# 是 简单的, 编译器 和 ID ...

  4. Django之 HelloWorld

    1.C:\Users\andy>django-admin startproject sundyblog   ## 创建项目     C:\Users\andy>cd sundyblog2. ...

  5. php 学习资料

    http://blog.csdn.net/woshihaiyong168/article/details/52846205  --队列基本原理的认识

  6. <ROS> NodeHandle句柄

    作者:jack_20 原文:https://blog.csdn.net/jack_20/article/details/70746736 1.句柄可以让你通过构造函数指定命名空间 ros::NodeH ...

  7. [原创] JAVA 递归线程池测试 ExecutorService / ForkJoinPool

    测试工具使用递归的方式获取子进程的Msg消息,目前有2种常用的ExecutorService / ForkJoinPool 为了测试哪种效果较好,我们来写个测试Demo,循环5555555次+1(加锁 ...

  8. 安装git-review

    参考 https://blog.csdn.net/qq18340811755/article/details/80965188 当yum install git-review安装失效,没有安装包时,只 ...

  9. sed命令的基本使用方法

    sed命令 stream editor,用程序的方式编辑文本.基本上是玩正则模式匹配. 用s命令替换 $ sed "s/my/Hao Chen's/g" pets.txt 单引号去 ...

  10. 阿里云ssl负载均衡证书配置

    https://www.chinassl.net/ssl_install/n683.html