SpringBean生命周期及类加载器简述

Spring bean 从jvm加载class初使化至进程结束bean消亡全局流程大致如下,同步的相关的简单验证

SpringBean生命周期及类加载器简述


 

public class TestBean implements InitializingBean, DisposableBean {

   
public TestBean() {
       
System.out.println("1: constructor ----> 构造方法");
   
}

   
@PostConstruct
   
public void postConstruct()
    {
       
System.out.println("3: @PostConstruct/postConstruct ");
   
}

   
@PreDestroy
   
public void PreDestroy()
    {
       
System.out.println("7: @PreDestroy/PreDestroy ");
   
}

   
public void initMethod() {
       
System.out.println("5: initMethod指定的initMethod方法");
   
}

   
public void destroyMethod() {
       
System.out.println("9: destroyMethod指定的destroyMethod方法");
   
}

   
@Override
   
public void destroy() throws Exception {
       
System.out.println("8: DisposableBean/destroy");
   
}

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

 

public class TestBeanPostPorcesser implements BeanPostProcessor {

   
@Override
   
public Object postProcessBeforeInitialization(Object bean, String beanName)
       
throws BeansException {
       
if(beanName.equals("testBean")) {
           
System.out.println("2: BeanPostProcessor/postProcessBeforeInitialization: " + beanName);
       
}
       
return bean;
   
}

   
@Override
   
public Object postProcessAfterInitialization(Object bean, String beanName)
       
throws BeansException {
       
if(beanName.equals("testBean")) {
           
System.out.println("6: BeanPostProcessor/postProcessAfterInitialization: " + beanName);
       
}
       
return bean;
   
}
}

 

 

@Configuration
public class TestConfig {

   
@Bean(initMethod = "initMethod",destroyMethod = "destroyMethod")
   
public TestBean testBean() {
       
return new TestBean();
   
}

   
@Bean
   
public TestBeanPostPorcesser testBeanPostPorcesser() {
       
return new TestBeanPostPorcesser();
   
}
}
 
 
********************************************************************
1: constructor ----> 构造方法
2: BeanPostProcessor/postProcessBeforeInitialization: testBean
3: @PostConstruct/postConstruct 
4: InitializingBean/afterPropertiesSet......
5: initMethod指定的initMethod方法
6: BeanPostProcessor/postProcessAfterInitialization: testBean
2021-03-20 22:53:18.404  INFO 7492 --- [           main] 
o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 
7: @PreDestroy/PreDestroy 
8: DisposableBean/destroy
9: destroyMethod指定的destroyMethod方法
Process finished with exit code 130

 

 

类加载器

Ø  类加载器用来加载其他类,类加载器本身无需加载,嵌套在虚拟机上即 BootStrap

Ø  类加载器将类的字节码文件从硬盘加载进内存

Ø  系统默认三个主要类加载器,每个类负责加载特定位置:  BootStrap, ExtClassLoader,AppClassLoade(SystemCLassLoade) 


 SpringBean生命周期及类加载器简述

Springboot Aware接口

SpringBean生命周期及类加载器简述

上一篇:for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本


下一篇:业务数据迁移至ES方案