spring中bean初始化执行顺序

常用的javabean的初始化方法为,构造方法,@PostConstruct,以及实现InitializingBean接口的afterPropertiesSet方法。
note在构造方法执行时候,spring还没有对bean中注入属性,即是@autowired还没有生效。
@PostConstruct,以及实现InitializingBean接口的afterPropertiesSet方法,都是在@autowired生效后。
接下来研究三个方法的执行顺序。

@Service
public class AService extends FunService {

    public AService(){
        System.out.println("构造方法");
    }

    @PostConstruct
    private void init(){
        System.out.println("PostConstruct");
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterProperties");
    }
}

执行结果:

构造方法
PostConstruct
afterProperties

posted @ 2020-08-20 19:09  刃牙  阅读(1374)  评论(0编辑  收藏  举报