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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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--------------");
    }
 
 
}
————————————————
版权声明:本文为CSDN博主「Starting Coding」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44128511/article/details/100119093

  

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

---------------hello constructor-----------
----------postConstructor--------------
----------hello init-----------
1
2
3
即 servlet启动过程,启动顺序 constructor > PostConstruct > Servlet的init()

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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注册

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
————————————————
版权声明:本文为CSDN博主「Starting Coding」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44128511/article/details/100119093

posted @   A汉克先生  阅读(407)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示