Backend Development 16 min read

A Comprehensive Guide to Spring Boot Startup Sequence and Extension Points

This article explains the Spring Boot startup process, enumerates the key extension points and their execution order, demonstrates the sequence with practical code examples, and answers common questions about bean initialization, lifecycle callbacks, and the proper timing for opening RPC, MQ, and HTTP traffic.

Top Architect
Top Architect
Top Architect
A Comprehensive Guide to Spring Boot Startup Sequence and Extension Points

Although most Java developers spend their time on CRUD operations, understanding the Spring framework's startup mechanism is essential for integrating middleware such as RPC, MQ, and embedded Tomcat correctly.

Overview of Spring Startup Order

A long diagram (not shown) illustrates the complete startup sequence. The article then poses eleven questions to test readers' grasp of when various components are registered during Spring's boot process.

Can getBeanByAnnotation be called in @PostConstruct and return accurate results?

How should a project listen to Spring's ready event?

How to listen to Spring's refresh event?

What is the execution order and difference between Spring ready and refresh events?

When does HTTP traffic start?

Is it reasonable to register RPC in an init-method ?

Is it reasonable to register an MQ consumer in an init-method ?

When are @PostConstruct , afterPropertiesSet , and init-method executed?

Will Spring always execute a bean's @PostConstruct before another bean's init-method ?

When are @Autowired fields injected relative to @PostConstruct ?

What is the order between ApplicationContextAware and @PostConstruct ?

Key Extension Points

BeanFactoryAware

ApplicationContextAware

BeanNameAware

ApplicationListener (e.g., ContextRefreshedEvent)

CommandLineRunner

SmartLifecycle#start

@PostConstruct

InitializingBean

init-method

Configuration @Bean methods

BeanPostProcessor (before & after initialization)

BeanFactoryPostProcessor

Code Experiment to Verify Order

The following class implements many lifecycle interfaces and logs each callback:

public class TestSpringOrder implements ApplicationContextAware,
      BeanFactoryAware,
      InitializingBean,
      SmartLifecycle,
      BeanNameAware,
      ApplicationListener
,
      CommandLineRunner,
      SmartInitializingSingleton {
    // ... method implementations that log the execution point
}

A second class demonstrates @PostConstruct and init-method :

@PostConstruct
public void postConstruct() {
    log.error("启动顺序:post-construct");
}

public void initMethod() {
    log.error("启动顺序:init-method");
}

A third class implements BeanPostProcessor and BeanFactoryPostProcessor to log before/after initialization:

public class TestSpringOrder3 implements BeanPostProcessor, BeanFactoryPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        log.error("启动顺序:BeanPostProcessor postProcessBeforeInitialization beanName:{}", beanName);
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        log.error("启动顺序:BeanPostProcessor postProcessAfterInitialization beanName:{}", beanName);
        return bean;
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        log.error("启动顺序:BeanFactoryPostProcessor postProcessBeanFactory ");
    }
}

Actual Execution Log

2023-11-25 18:10:53,748 [main] ERROR (TestSpringOrder3:37) - 启动顺序:BeanFactoryPostProcessor postProcessBeanFactory 
2023-11-25 18:10:59,299 [main] ERROR (TestSpringOrder:53) - 启动顺序: 构造函数 TestSpringOrder
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder:127) - 启动顺序: Autowired
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder:129) - 启动顺序:setBeanName
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder:111) - 启动顺序:setBeanFactory
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder:121) - 启动顺序:setApplicationContext
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder3:25) - 启动顺序:BeanPostProcessor postProcessBeforeInitialization beanName:testSpringOrder
2023-11-25 18:10:59,316 [main] ERROR (TestSpringOrder:63) - 启动顺序:post-construct
2023-11-25 18:10:59,317 [main] ERROR (TestSpringOrder:116) - 启动顺序:afterPropertiesSet
2023-11-25 18:10:59,317 [main] ERROR (TestSpringOrder:46) - 启动顺序:init-method
2023-11-25 18:10:59,320 [main] ERROR (TestSpringOrder3:31) - 启动顺序:BeanPostProcessor postProcessAfterInitialization beanName:testSpringOrder
2023-11-25 18:17:21,563 [main] ERROR (SpringOrderConfiguartion:21) - 启动顺序: @Bean 注解方法执行
2023-11-25 18:17:21,668 [main] ERROR (TestSpringOrder:58) - 启动顺序:SmartInitializingSingleton
2023-11-25 18:17:21,675 [main] ERROR (TestSpringOrder:74) - 启动顺序:start
2023-11-25 18:17:23,508 [main] ERROR (TestSpringOrder:68) - 启动顺序:ContextRefreshedEvent
2023-11-25 18:17:23,574 [main] ERROR (TestSpringOrder:79) - 启动顺序:CommandLineRunner

Key Takeaways

BeanFactoryPostProcessor runs after bean definitions are loaded but before any bean instances are created.

Bean instantiation occurs via reflection (e.g., new TestSpringOrder() ).

@Autowired injection happens after bean creation and before @PostConstruct , so fields are never null in @PostConstruct .

Aware interfaces ( BeanNameAware , BeanFactoryAware , ApplicationContextAware ) are called before bean initialization callbacks.

BeanPostProcessor methods surround the bean's own initialization methods.

The order of @PostConstruct , afterPropertiesSet , and init-method is: @PostConstruct → afterPropertiesSet → init-method .

Spring does not guarantee a fixed order between different beans unless @Order is used.

All singleton beans are fully initialized before SmartInitializingSingleton runs; this is the safest point to start RPC, MQ, or HTTP traffic.

Spring Boot opens the embedded servlet container (Tomcat/Jetty) only after the application context is ready, i.e., after SmartLifecycle#start .

Listeners such as CommandLineRunner execute after the entire context is up, which may be too late for cache warm‑up.

Answers to Frequently Asked Questions

Q: Execution order of init-method , @PostConstruct , afterPropertiesSet ? A: @PostConstruct → afterPropertiesSet → init-method .
Q: If two beans have different initialization declarations, does Spring always run the @PostConstruct bean first? A: No. Spring initializes beans one by one; the order between beans is undefined unless explicitly ordered.
Q: When is @Autowired performed relative to @PostConstruct ? A: Injection occurs before @PostConstruct , so the field is available.
Q: Which event should be used to listen for Spring readiness? A: Implement SmartLifecycle and use its start() method.

In summary, to avoid runtime failures, developers should defer opening RPC, MQ, and HTTP traffic until after SmartInitializingSingleton or the Spring ready event, ensuring all beans and event listeners are fully registered.

JavaSpringLifecycleSpringBootBeanstartupExtensionPoints
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.