Why recommend everyone to learn bytecode?

1. Background

This article will combine a simple case to learn the specific content of the initialization and destruction phases in the Bean life cycle.

2. Analysis

Guess first and then verify: instantiation, property filling, initialization, destruction. Specifically, whether the initialization method that implements the interface is executed first or the custom initialization method is executed first depends on the code.

We use the break debugging method to analyze and learn, and put breakpoints on each method for debugging.
[1] Execution to the construction method
[2] Execute to the setValue method
【3】Execute to the InitializingBean#afterPropertiesSet method
[4] Execute to init-method
Core method AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
//1 Preparation before initialization
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
//2 Get BeanFactory, load all bean definition information (not instantiated)
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
// 3 BeanFactory preprocessing configuration
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
// 4. Prepare for post-processing after the BeanFactory is completed
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
// 5. Execute the post-processor after the BeanFactory is created
invokeBeanFactoryPostProcessors(beanFactory);
[1] Instantiation, [2] Property filling, [3] afterPropertiesSet, [4] Custom init method are all in step 11.

Core method:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

【5】 Execute to DisposableBean#destroy destruction method

3. Summary
This article mainly uses the method of guessing and verification, and the method of code debugging to learn the initialization and destruction of the Bean life cycle.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us