一、写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入

package com.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class SpringUtil implements ApplicationContextAware
{ private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
if (SpringUtil.applicationContext == null)
{
SpringUtil.applicationContext = applicationContext;
} //"ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="
} // 获取applicationContext
public static ApplicationContext getApplicationContext()
{
return applicationContext;
} // 通过name获取 Bean.
public static Object getBean(String name)
{
return getApplicationContext().getBean(name);
} // 通过class获取Bean.
public static <T> T getBean(Class<T> clazz)
{
return getApplicationContext().getBean(clazz);
} // 通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz)
{
return getApplicationContext().getBean(name, clazz);
} }

二、在拦截器中若自动注入没有生效,需要手动判断后,重新赋值

package com.webconfig;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.entity.TokenEntity;
import com.repository.TokenRepository;
import com.util.CommonUtil; import sun.misc.BASE64Decoder; @Component
public class TokenInterceptor implements HandlerInterceptor
{ private final static BASE64Decoder decode = new BASE64Decoder();
@Autowired
private TokenRepository tokenRep; // 在请求处理之前进行调用(Controller方法调用之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
String token = request.getParameter("token");
String msg = "";
boolean canFilter = false;
if (!CommonUtil.isNull(token))
{
String tokenStr = new String(decode.decodeBuffer(token));
String[] tokens = tokenStr.split("_");
if (tokens.length == 2)
{
if (tokenRep == null)
{
// 解决tokenRep为null无法注入问题
//System.out.println("TokenRepository is null!!!");
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
tokenRep = (TokenRepository) factory.getBean("tokenRepository");
}
String userName = tokens[0];
String curToken = tokens[1];
TokenEntity tokenInfo = tokenRep.findByUserName(userName);
if (tokenInfo != null)
{
if (curToken.equals(tokenInfo.getToken()))
{
tokenInfo.setCreatTime(new Date());
tokenRep.save(tokenInfo);
canFilter = true;
} else
{
msg = "The user is logged in elsewhere and token has failed";
}
} else
{
msg = "Token has expired,please get it again";
}
} else
{
msg = "token fomart incorrect";
} } else
{
msg = "token is empty";
} if (canFilter)
{
return true;
} else
{
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().println(msg);
}
return false;
} // 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception
{
// TODO Auto-generated method stub } // 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception
{
// TODO Auto-generated method stub } }

springboot拦截中自动注入的组件为null问题解决方法的更多相关文章

  1. 关于springboot项目中自动注入,但是用的时候值为空的BUG

    最近想做一些web项目来填充下业余时间,首先想到了使用springboot框架,毕竟方便 快捷 首先:去这里 http://start.spring.io/ 直接构建了一个springboot初始化的 ...

  2. 带你开发一款给Apk中自动注入代码工具icodetools(完善篇)【申明:来源于网络】

    带你开发一款给Apk中自动注入代码工具icodetools(完善篇)[申明:来源于网络] 带你开发一款给Apk中自动注入代码工具icodetools(完善篇):http://blog.csdn.net ...

  3. SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案

    错误: 在Springboot  框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...

  4. SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成增删改查逻辑

    习惯使用jpa操作对象的方式,现在用mybatis有点不习惯. 其实是懒得写SQL,增删改查那么简单的事情你帮我做了呗,mybatis:NO. 没办法,自己搞喽! 这里主要是实现了通过代码自动生成my ...

  5. SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成ResultMap

    其实我一点都不想用mybatis,好多地方得自己写,比如这里. 使用mybatis要写大量的xml,烦的一批.最烦人的莫过于写各种resultmap,就是数据库字段和实体属性做映射.我认为这里应该是m ...

  6. Springboot 拦截器 依赖注入失败

    解决方案2种. ====1 https://blog.csdn.net/shunhua19881987/article/details/78084679 ====2 https://www.cnblo ...

  7. System.Runtime.InteropServices.COMException 检索COM类工厂中CLSID{xxxxxxxxx}的组件时失败解决方法

    iis7.5中设定应用程序池中<进程模型>中<标识>为localSystem 提示:System.Runtime.InteropServices.COMException: 命 ...

  8. Selenium中自动输入10位随机数字的方法

    有时候项目中需要输入快递号,对于已输入过的快递单号则不能再次输入,这种问题怎么解决呢,可以看下这个方法 public static final String ALLCHAR = "01234 ...

  9. Idea中自动生成get,set,toString等方法

    https://blog.csdn.net/sinat_41226396/article/details/80770520 自动生成main函数: https://blog.csdn.net/fanr ...

随机推荐

  1. 10.矩形覆盖 Java

    题目描述 我们可以用2**1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法? 思路 其实,倒数第一列要么就是1个2**1的矩形竖着放,要么就 ...

  2. FinalCutPro快捷键

    FinalCutPro快捷键使用 FinalCutPro的快捷键使用十分有用,特对一些基本的快捷键进行了总结 1)i:截取片段开始Initial 2)o: 截取片段结束Over i和o可以在一个素材片 ...

  3. Linux上安装Python3

    1. 安装支持包 yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel ope ...

  4. 码云上webide怎么提交

    修改后想提交,它会提示:“暂存文件后才能提交”, 我拿放大镜找遍了整个界面也没找到“暂存”按钮, 原来,文件旁边那个+号就是暂存,好歹鼠标方式去之后给个tip,服了. 点一下这个加号,提交按钮就可用了 ...

  5. 服务安全-IAM:百科

    ylbtech-服务安全-IAM:百科 IAM(身份识别与访问管理(简称大4A)) IAM(Identity and Access Management 的缩写),即“身份识别与访问管理”,具有单点登 ...

  6. ControlTemplate in WPF —— Calendar

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  7. Pycharm从虚拟环境切换到本地环境

    切换到本地: 点击左上角File – settings , 在打开的对话框中选择:Project xxx (xxx是你项目的名称) – Project Interpreter ,在右边可以看到解释器, ...

  8. KVM虚拟化原理

    CPU虚拟化 KVM虚拟化是需要硬件支持的.我们可以用 egrep -o '(vmx|svm)' /proc/cpuinfo 来查看是否支持CPU虚拟化. 虚拟机中每一个vCPU对应qemu-kvm中 ...

  9. 在Debian下利用URLOS快速安装SqlServer2017

    SqlServer能在Debian上安装吗?答案是可以!网络上也能找到很多Linux系统下安装SqlServer的相关文章,也许经过一些折腾,你也能成功在Debian中安装sqlserver,但是其中 ...

  10. Linux C/C++基础 文件(中)

    1.ubuntu cat命令的实现 cat——查看或者合并文件内容 #include<stdio.h> int main(int argc,char* argv[]) { //1.打开文件 ...