一、写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. 如何阅读《JavaScript高级程序设计》(一)

    题外话 最近在看<JavaScript高级程序设计>这本书,面对着700多页的厚书籍,心里有点压力,所以我决定梳理一下..探究一下到底怎么读这本书.本书的内容好像只有到ES5...所以只能 ...

  2. 【Nginx】 linux环境下安装nginx步骤

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. centos平台编译环境使用如下指令 安装make: yum -y install gcc automake autoconf libto ...

  3. Nginx-rtmp之 ngx_rtmp_send.c 文件分析

    1. 简述 1.1 RTMP 消息类型 /* RTMP message types */ #define NGX_RTMP_MSG_CHUNK_SIZE 1 #define NGX_RTMP_MSG_ ...

  4. VLC-Qt 入门指南

      关于 VLC-Qt VLC-Qt:一个结合了 Qt 应用程序和 libVLC 的免费开源库.它包含了用于媒体播放的核心类,以及用于更快速地进行媒体播放器开发的一些 GUI 类. 官网地址:http ...

  5. 显示Pl/Sql Developer window list窗口

    默认情况下Window List窗口是不显示的,这十分不方便 (一)在菜单项的Tools下的Preference选项中的UserInterface中选择Option,在右边对于的Autosave de ...

  6. [go]go并发

    同步协程 通过睡眠方法 // 通过睡眠方式等待 time.Sleep(time.Second) <-time.NewTimer(time.Second).C <-time.After(ti ...

  7. Linux之tar命令

    命令格式: tar [-cxzjvf]   压缩打包文档的名称 欲打包目录 参数: -c :建立一个归档文件的参数指令 -x :解开一个归档文件的参数指令! -z :是否需要用 gzip 压缩? -j ...

  8. mongdb aggregate聚合操作

    1.数据准备 查看前一篇group操作 2.aggregate函数参数讲解 mysql mongdb===================WHERE --->$match GROUP BY -- ...

  9. 前端Ajax跨域解决方案

    业务场景: 前后端分离需要对接数据接口. 接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比较多)所以在此记录分享我的踩坑之旅,以便能更好的理解,应 ...

  10. 无缝滚动的float属性

    The float CSS property specifies that (1) an element should be taken from the normal flow and (2) pl ...