Spring Bean初始化的几种方法以及执行顺序

  • @PostConstruct 构造后置执行
  • @Bean(initMethod="xxx") 初始化功能
  • @InitializingBean#afterPropertiesSet 属性填充后执行

DefaultRumenzFactory.java

package com.rumenz;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;

public class DefaultRumenzFactory implements  InitializingBean {

    public DefaultRumenzFactory() {
        System.out.println("无参构造方法执行....");
    }

    @PostConstruct
    public void init(){
        System.out.println("PostConstruct init.......");
    }
    public void initMethod(){
        System.out.println("init method.......");
    }

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

DemoApplication.java调用

package com.rumenz;


import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class DemoApplication {
    public static  void main(String[] args) {
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
        ac.register(DemoApplication.class);
        ac.refresh();
        DefaultRumenzFactory bean = ac.getBean(DefaultRumenzFactory.class);
        ac.close();
    }
    @Bean(initMethod = "initMethod")
    public static  DefaultRumenzFactory defaultRumenzFactory(){
        return new DefaultRumenzFactory();
    }

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'defaultRumenzFactory'
无参构造方法执行....
PostConstruct init.......
afterPropertiesSet.....
init method.......

执行顺序
1.@PostConstruct
2.@InitializingBean#afterPropertiesSet
3.@Bean(initMethod="xxx")

源码:https://github.com/mifunc/Spring-BeanInitialization

image.png

原文: https://rumenz.com/rumenbiji/Spring-BeanInitialization.html

你可能感兴趣的:(Spring Bean初始化的几种方法以及执行顺序)