虽然 @PostConstruct、 @Autowired 和 构造函数都是在 Servlet 加载时调用,但是调用是时间还是有一些区别
一、当 @PostConstruct、 @Autowired 和 构造函数 在同一个类中的时候
调用的顺序为: 构造函数 > @Autowired > @PostConstruct
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Auther: yoult
* @Description: TODO 初始化函数运行顺序测试
* @LoginName: ZDGG
* @Date: 2020-12-14 22:01 星期一
*/
@Component
public class SuccessivelyTestA {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式,精确到毫秒
SuccessivelyTestA(){
System.out.println(df.format(new Date()) + "==>successivelyTestA");
}
@PostConstruct
void testPostConstruct(){
System.out.println(df.format(new Date()) + "==>testPostConstructA");
}
@Autowired
private void testAutowired(Test test){
System.out.println(df.format(new Date()) + "==>testAutowiredA");
}
}
import org.springframework.stereotype.Component;
/**
* @Auther: yoult
* @Description: TODO
* @LoginName: ZDGG
* @Date: 2020-12-14 22:01 星期一
*/
@Component
public class Test {
Test(){
}
}
运行结果为:
2020-12-14 22:36:18.361==>successivelyTestA
2020-12-14 22:36:18.364==>testAutowiredA
2020-12-14 22:36:18.364==>testPostConstructA
二、当 @PostConstruct、 @Autowired 和 构造函数 在不同的类中时
现有SuccessivelyTestA 类和 SuccessivelyTestB 类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Auther: yoult
* @Description: TODO 初始化函数运行顺序测试
* @LoginName: ZDGG
* @Date: 2020-12-14 22:01 星期一
*/
@Component
public class SuccessivelyTestB {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式,精确到毫秒
SuccessivelyTestB(){
System.out.println(df.format(new Date()) + "==>successivelyTestB");
}
@PostConstruct
void testPostConstruct(){
System.out.println(df.format(new Date()) + "==>testPostConstructB");
}
@Autowired
private void testAutowired(Test test){
System.out.println(df.format(new Date()) + "==>testAutowiredB");
}
}
2.1 当类 SuccessivelyTestA 和类 SuccessivelyTestB 无关联时,按类加载顺序执行
运行结果为:
2020-12-14 22:44:01.711==>successivelyTestA
2020-12-14 22:44:01.714==>testAutowiredA
2020-12-14 22:44:01.714==>testPostConstructA
2020-12-14 22:44:01.714==>successivelyTestB
2020-12-14 22:44:01.715==>testAutowiredB
2020-12-14 22:44:01.715==>testPostConstructB
2.2 若在SuccessivelyTestA类 中注入 SuccessivelyTestB 类时 ,则执行顺序为: A类构造方法 > B类构造方法 > B类 @Autowired > B类 @PostConstruct > A类 @Autowired > A类 @PostConstruct
/**
* @Auther: yoult
* @Description: TODO 初始化函数运行顺序测试
* @LoginName: ZDGG
* @Date: 2020-12-14 22:01 星期一
*/
@Component
public class SuccessivelyTestA {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式,精确到毫秒
SuccessivelyTestA(){
System.out.println(df.format(new Date()) + "==>successivelyTestA");
}
@PostConstruct
void testPostConstruct(){
System.out.println(df.format(new Date()) + "==>testPostConstructA");
}
/**
* 此处把注入类改为 SuccessivelyTestB
* @param successivelyTestB
*/
@Autowired
private void testAutowired(SuccessivelyTestB successivelyTestB){
System.out.println(df.format(new Date()) + "==>testAutowiredA");
}
}
运行结果为:
2020-12-14 22:53:02.072==>successivelyTestA
2020-12-14 22:53:02.074==>successivelyTestB
2020-12-14 22:53:02.076==>testAutowiredB
2020-12-14 22:53:02.076==>testPostConstructB
2020-12-14 22:53:02.077==>testAutowiredA
2020-12-14 22:53:02.077==>testPostConstructA