Spring @Autowired注解在非Controller中注入为null
问题描述
今天在写一个工具类,里面用了@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的更多相关文章
- Spring boot @Autowired注解在非Controller中注入为null
参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777
- Spring @Autowired注解在非Controller/Service中注入为null
参考:https://blog.csdn.net/qq_35056292/article/details/78430777 问题出现: 在一个非controller/service类中,我需要注入Co ...
- 【转】在SpringMVC Controller中注入Request成员域
原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...
- 在SpringMVC Controller中注入Request成员域
主题 在工作中遇到1个问题....我们定义了一个Controller基类,所有Springmvc自定义的controller都继承它....在它内部定义一个@Autowired HttpServlet ...
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
- spring aop在mvc的controller中加入切面无效
spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...
- Spring @Autowired 注解 学习资料
Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...
- Spring @Autowired 注解自动注入流程是怎么样?
面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
随机推荐
- ImportError: dynamic module does not define module export function (PyInit__sqlite3)
使用python3.6 中的django-admin创建项目的时候报错 ImportError: dynamic module does not define module export functi ...
- [工作积累] NBA 2K16 mobile终于发布了
忙了大半年, 终于出来了. iOS: https://itunes.apple.com/us/app/nba-2k16/id984732818?mt=8 Android: https://play.g ...
- C++ Boost库分类总结
c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...
- java数据结构之链表(java核心卷Ⅰ读书笔记)
1.链表 数组和ArrayList的一个重大缺陷就是:从中间位置删除一个元素要付出很大的代价,因为在这个元素删除之后,所有的元素都要向前端移动,在中间的某个位置插入一个元素也是这个原因. (小感悟:s ...
- Rocketlab公司火箭Electron介绍
http://https://en.wikipedia.org/wiki/Rocket_Lab https://www.rocketlabusa.com/ Rocketlab(火箭实验室)是一家致力于 ...
- mac 版本navicate 如何安装破解版
https://www.jianshu.com/p/f42785e55b6b 博客地址 部分童鞋安装后没有rpk文件,我也不知道怎么解决实在不行,请下载破解版链接:https://pan.baidu ...
- magento 1.9 上传后图片前后台无法正常显示
1.上传后图片不显示,设置 允许 flash 2.保证php 执行是内存大小至少为为128M,多种方式设置,这里以init_set为例子,在index.php 加入下面一行代码,根据情况而定 ini_ ...
- python获取机器信息脚本(网上寻找的)
获取机器信息(待测试) # -*- coding: UTF-8 -*- import psutil import json import os import socket import struct ...
- 记一次nmap扫描信息收集过程
1.首先扫描网段内存活主机 nmap -sP 192.168.1.1-254 2.获取到存活主机后,开始进行端口扫描 nmap -p1-1000 192.168.1.66 可得到端口状态: filte ...
- WMware 中CentOS系统Hadoop 分布式环境搭建(一)——Hadoop安装环境准备
1.创建3台虚拟机并装好系统,这里使用64位CentOS. 2.Ping测试[确保两两能ping通]: [ping xxx.xxx.xxx.xxx] 3.安装SSH:[yum install ssh ...