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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM