大佬们总结出现的原因:
拦截器加载的时间点在springcontext之前,所以在拦截器中注入自然为null。
二话不说上代码
首先这个是自定义拦截器
package com.csf.interceptor;
import com.csf.interceptor.dto.Wbjnl;
import com.csf.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyInterceptor implements HandlerInterceptor {
@Autowired
private DemoService demoService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle被调用");
Wbjnl wbjnl = new Wbjnl();
wbjnl.setContent("张三和李四来了");