Mastering Spring Boot Startup: 11 Critical Questions & Execution Order

This article provides a deep technical analysis of Spring Boot's startup process, explains every relevant extension point, demonstrates how to log and verify execution order with sample code, answers eleven common questions about bean initialization, and offers practical guidance on when to safely open RPC, MQ, and HTTP traffic.

Architect
Architect
Architect
Mastering Spring Boot Startup: 11 Critical Questions & Execution Order

Spring Startup Extension Points

BeanFactoryAware

– obtain the BeanFactory instance. ApplicationContextAware – obtain the ApplicationContext instance. BeanNameAware – get the bean’s name in the IoC container. ApplicationListener – listen for events such as ContextRefreshedEvent. CommandLineRunner – run after the entire application context is ready. SmartLifecycle#start – invoked after all singleton beans are instantiated. @PostConstruct – bean initialization hook. InitializingBean – another bean initialization hook.

XML init‑method – bean initialization hook defined in XML. @Configuration class with @Bean – registers beans programmatically. BeanPostProcessor – custom logic before and after bean initialization. BeanFactoryPostProcessor – runs after the BeanFactory is created but before any bean instances.

Demonstration of the Real Execution Order

The following test class implements many of the above interfaces and logs a message in each callback.

public class TestSpringOrder implements ApplicationContextAware, BeanFactoryAware, InitializingBean, SmartLifecycle, BeanNameAware, ApplicationListener<ContextRefreshedEvent>, CommandLineRunner, SmartInitializingSingleton {
    @Override
    public void afterPropertiesSet() throws Exception { log.error("启动顺序:afterPropertiesSet"); }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { log.error("启动顺序:setApplicationContext"); }
    // other overridden methods omitted for brevity
}

A @PostConstruct method and an XML init‑method are added to the same bean.

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

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

Running the application produces the following log (excerpt):

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 Answers to Common Spring Startup Questions

Execution order of initialization callbacks : @PostConstruct → afterPropertiesSet → init‑method.

Does Spring guarantee that a bean using @PostConstruct runs before a bean using init‑method ? No. Beans are initialized individually; ordering is undefined unless an explicit @Order is applied.

When are @Autowired fields injected? Injection occurs before any @PostConstruct method, so a @PostConstruct method sees a fully injected bean (unless the dependency is optional and missing).

Order of ApplicationContextAware and @PostConstruct : ApplicationContextAware is invoked first, guaranteeing a non‑null ApplicationContext inside @PostConstruct.

How to listen for Spring readiness : Implement SmartLifecycle and use its start() method, or listen for SmartInitializingSingleton.

How to listen for Spring refresh events : Register an ApplicationListener<ContextRefreshedEvent>.

Ready event vs. refresh event : The ready event fires earlier; both may be emitted multiple times, so listeners should be idempotent.

When does HTTP traffic become available? After the Spring context is fully built and SmartLifecycle#start has executed, the embedded Tomcat/Jetty opens the port.

Is registering RPC in init‑method reasonable? No – at that point Spring has not finished event registration, so RPC may miss events.

Is registering MQ in init‑method reasonable? No – the same reasoning applies; consumers should start after Spring is fully ready.

Can getBeanByAnnotation be called in @PostConstruct ? Yes – Spring will lazily instantiate any missing beans needed for the query, returning accurate results.

Source‑Level Details

SmartInitializingSingleton Execution Position

After all singleton beans are created, Spring invokes SmartInitializingSingleton callbacks.

SmartInitializingSingleton diagram
SmartInitializingSingleton diagram

Autowired Injection Timing

After bean instantiation but before any @PostConstruct method, AutowiredAnnotationBeanPostProcessor injects @Autowired fields.

Autowired injection timing
Autowired injection timing

Spring Boot HTTP Port Opening

Spring Boot first creates the Spring context, then initializes Spring MVC, and finally starts the embedded web server to listen on the port.

Spring Boot HTTP startup
Spring Boot HTTP startup

Core Bean Initialization Code

The central method is AbstractAutowireCapableBeanFactory#initializeBean, which orchestrates all initialization callbacks.

Bean initialization code
Bean initialization code

CommandLineRunner Execution Position

After the Spring context is fully built, Spring Boot invokes any CommandLineRunner beans.

CommandLineRunner timing
CommandLineRunner timing

Practical Recommendations

Open HTTP, RPC, and MQ entry traffic only after SmartInitializingSingleton (or after the ContextRefreshedEvent ) to avoid missing Spring event registration.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMicroservicesSpring BootAutowiredbean-lifecyclePostConstructCommandLineRunnerInitMethodSmartInitializingSingletonStartup Sequence
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

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.