问题:在拦截器中注入service或者dao会空指针
分析:出现这个问题的原因是因为拦截器作用在属性自动注入开始之前,其运行时,内部属性还未开始注入,所以为空
解决:在拦截器中加入该方法,通过该方法获取bean对象
private <T> T getBean(Class<T> clazz, HttpServletRequest request){ WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); return applicationContext.getBean(clazz); }
标签:applicationContext,拦截器,service,dao,getBean,clazz From: https://www.cnblogs.com/DarkerbeS/p/16918440.html