Spring详解:WebServlet 中不能注入Bean对象

1. 前言

最近在研究Spring IOC、AOP以及和Mybatis整合的时候发现在Spring中使用Servlet+Service+Dao(Mybatis)的时候,发现在Controller层也就是Servlet中不能通过@Autowired注入Bean对象。这个时候我就纳闷了,在Spring中明明对象的创建和管理交给Spring IOC容器去管理,纳闷为什么不能再容器中注入Bean对象? 下面我们贴出问题代码:

@WebServlet(value = "/servlet/AirportServelt")
public class AirportServelt extends HttpServlet {

    @Autowired
    private AirportService airportService;

    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //报错NullPointExecuption异常, airportService这个类null指针
        List<Airport> allAirport = airportService.findAllAirport();
        req.setAttribute("airPortList",allAirport);
        System.out.println(allAirport);
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
}

2. Spring WebServlet中不能注入Bean原理

想了解此问题的原理,就要了解tomcat启动后 servlet和spring的加载顺序。在TomcatWeb 容器加载一个Web项目的时候:

  1. tomcat启动后先加载web.xml文件。web.xml主要配置了servlet 、filter、listenner三种javaEE规范的类,加载顺序跟在web.xml文档中的位置无关。顺序为 listenner>filter>servlet 。
  2. 而spring的初始化类为org.springframework.web.context.ContextLoaderListener,就是一个listenner,它是先于servlet加载的。普通servlet和springmvc的入口servlet的加载顺序,就要看servlet的设置了。它们按照在Web.xml中定义的Servlet顺序加载。其中springmvc需要指定org.springframework.web.servlet.DispatcherServlet拦截所有的Web请求。
  3. 在 servletA类上加@WebServlet等注解时,spring或springmvc会扫面相关包,自动实例化一个servlet实例A;这个实例A的引用是spring IOC容器管理的。这个时候Spring ContextLoaderListener监听器首先初始化,扫描所有的java包,创建Bean对象。然后Tomcat容器在加载Servlet类,包括我们定义的Servlet以及Spring的DispatcherServlet。
  4. Tomcat容器接下来会在web.xml配置加载Servlet类,这个时候加载DispatcherServle以及我们定义的Servlet类。这是tomcat容器会根据servler配置启动时或者第一次请求该url时实例化我们定义的Web servlet实例B.这个实例B的引用是tomcat容器管理的。
  5. 所以最终结果就是:拦截url的servlet和spring依赖注入的servlet不是同一个实例!!所以就产生了不能依赖注入或者注解不起作用的现象。

3. 解决方法

3.1 利用在Servlet中init()方法,重新注入Bean

@WebServlet(value = "/servlet/AirportServelt")
public class AirportServelt extends HttpServlet {

    @Autowired
    private AirportService airportService;

    @Override
    public void init() throws ServletException {
        //SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,getServletContext());
        WebApplicationContext webApplicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        airportService=webApplicationContext.getBean("airportService",AirportService.class);
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List allAirport = airportService.findAllAirport();
        req.setAttribute("airPortList",allAirport);
        System.out.println(allAirport);
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
}

3.2 servlet init方法里加入spring根据注解注入属性的方法

@WebServlet(value = "/servlet/AirportServelt")
public class AirportServelt extends HttpServlet {

    @Autowired
    private AirportService airportService;

    @Override
    public void init() throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,getServletContext());
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Airport> allAirport = airportService.findAllAirport();
        req.setAttribute("airPortList",allAirport);
        System.out.println(allAirport);
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
}

你可能感兴趣的:(Spring框架,Spring框架)