@PostConstruct、Servlet的init()方法、构造器、spring的autowired的执行顺序

一 servlet启动过程中的几项执行顺序

创建servlet类,并继承HttpServlet
首先测试servlet启动过程中 @PostConstruct、Servlet的init()方法、构造器的启动顺序

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet(name="MonitorDataCenter",urlPatterns = "/MonitorDataCenter",loadOnStartup=1)
public class testservlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("----------hello init-----------");
    }

    public servlet() {
        System.out.println("---------------hello constructor-----------");
    }
    @PostConstruct
    public void test1(){
        System.out.println("----------postConstructor--------------");
    }


}

@WebServlet中的loadOnStartup=1意思是随着servlet的启动而启动,启动程序,控制台输出

---------------hello constructor-----------
----------postConstructor--------------
----------hello init-----------

servlet启动过程,启动顺序 constructor > PostConstruct > Servlet的init()

测试servlet启动过程是否会完成autowired注入

import com.how2java.service.SystemService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet(name="MonitorDataCenter",urlPatterns = "/MonitorDataCenter",loadOnStartup=1)
public class servlet extends HttpServlet {
    @Autowired
    SystemService systemService;
    @Override
    public void init() throws ServletException {
        System.out.println("++++init中autowired+++++++++++++++"+systemService);
        System.out.println("----------hello init-----------");
        System.out.println();
    }

    public servlet() {
        System.out.println("++++constructor中autowired+++++++++++++++"+systemService);
        System.out.println("---------------hello constructor-----------");
        System.out.println();
    }
    @PostConstruct
    public void test1(){
        System.out.println("++++postConstructor中autowired+++++++++++++++"+systemService);
        System.out.println("----------postConstructor--------------");
        System.out.println();
    }
}

测试结果

++++constructor中autowired+++++++++++++++null
---------------hello constructor-----------

++++postConstructor中autowired+++++++++++++++null
----------postConstructor--------------

++++init中autowired+++++++++++++++null
----------hello init-----------

在servlet过程中都无法完成注入

二 spring注册过程中

然后使用@controller注解,使他可以被spring管理

import com.how2java.service.SystemService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

@Controller
public class servlet extends HttpServlet {
    @Autowired
    SystemService systemService;
    @Test
    public void test(){
        System.out.println(systemService);
    }
    public servlet() {
        System.out.println("++++constructor中autowired+++++++++++++++"+systemService);
        System.out.println("---------------hello constructor-----------");
        System.out.println();
    }
    @PostConstruct
    public void test1(){
        System.out.println("++++postConstructor中autowired+++++++++++++++"+systemService);
        System.out.println("----------postConstructor--------------");
        System.out.println();
    }
}

启动,控制台输出

++++constructor中autowired+++++++++++++++null
---------------hello constructor-----------

++++postConstructor中autowired+++++++++++++++com.how2java.service.impl.SystemServiceImpl@7eeaacc2
----------postConstructor--------------

意味着spring容器启动过程中,spring对bean进行注册过程中,autowired的注入处在构造器以后,postConstructor以前。
Constructor > autowired > PostConstruct

三 servlet和spring同时加载

同时使用@webservlet和@Controller注解,即同时进行servlet启动,spring注册


import com.how2java.service.SystemService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet(name="MonitorDataCenter",urlPatterns = "/MonitorDataCenter",loadOnStartup=1)
@Controller
public class servlet extends HttpServlet {
    @Autowired
    SystemService systemService;
    @Override
    public void init() throws ServletException {
        System.out.println("++++init中autowired+++++++++++++++"+systemService);
        System.out.println("----------hello init-----------");
        System.out.println();
    }

    public servlet() {
        System.out.println("++++constructor中autowired+++++++++++++++"+systemService);
        System.out.println("---------------hello constructor-----------");
        System.out.println();
    }
    @PostConstruct
    public void test1(){
        System.out.println("++++postConstructor中autowired+++++++++++++++"+systemService);
        System.out.println("----------postConstructor--------------");
        System.out.println();
    }
}

测试结果

++++constructor中autowired+++++++++++++++null
---------------hello constructor-----------

++++postConstructor中autowired+++++++++++++++null
----------postConstructor--------------

++++init中autowired+++++++++++++++null
----------hello init-----------

++++constructor中autowired+++++++++++++++null
---------------hello constructor-----------

++++postConstructor中autowired+++++++++++++++com.how2java.service.impl.SystemServiceImpl@30673fdd
----------postConstructor--------------

结果证明servlet和spring会分别执行class的构造器和postConstructor,但是servlet的启动会先于spring,即servlet > spring

你可能感兴趣的:(spring)