SpringBoot 学习之 @PostConstruct、 @Autowired与构造函数的执行顺序

虽然 @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
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值