Spring Boot’s Hidden Power: 10‑Step Bean Lifecycle Most Developers Miss

Most Spring Boot developers use @Service, @Component and @Autowired daily, yet few understand the ten‑step lifecycle a bean undergoes—from loading its definition to destruction—information that unlocks advanced features such as custom initialization, AOP, and dynamic proxy creation.

LuTiao Programming
LuTiao Programming
LuTiao Programming
Spring Boot’s Hidden Power: 10‑Step Bean Lifecycle Most Developers Miss

Many Spring Boot projects constantly use annotations like @Service, @Component and @Autowired, but the full lifecycle a bean experiences from creation to destruction is rarely explained.

The article breaks the lifecycle into ten distinct stages, each exposing a hook that can be customized for advanced capabilities such as custom initialization, dynamic proxying, and container‑aware logic.

1. Load BeanDefinition

During startup Spring scans the classpath for components ( @Component, @Service, @Repository, @Configuration) under /src/main/java/com/icoderoad, parses each class and creates a BeanDefinition that records the bean class, scope, dependencies and any init method.

2. Instantiate Bean

Spring uses reflection to create the bean instance:

Constructor<?> constructor = clazz.getDeclaredConstructor();
Object bean = constructor.newInstance();

For example, the OrderService class annotated with @Service is instantiated via new OrderService().

3. Dependency Injection

After the object is created, Spring injects dependencies marked with @Autowired:

field.set(orderService, userServiceBean);

4. BeanNameAware

If a bean implements BeanNameAware, Spring calls setBeanName(String name) to provide the bean’s name. Example prints Bean name: demoBean.

5. BeanFactoryAware

Implementing BeanFactoryAware gives the bean access to the container; the example prints BeanFactory injected.

6. BeanPostProcessor (before initialization)

Spring invokes postProcessBeforeInitialization() on all registered BeanPostProcessor s. This is the most powerful extension point, where AOP, transaction handling and proxy creation are typically applied.

7. InitializingBean

If the bean implements InitializingBean, Spring calls afterPropertiesSet() after property injection.

8. init‑method

A custom init method can be declared with @Bean(initMethod="init"). The method is invoked after InitializingBean processing.

9. BeanPostProcessor (after initialization)

Spring calls postProcessAfterInitialization(). Dynamic proxies for AOP are often generated here.

10. Bean Ready

The bean is now fully initialized and can be injected elsewhere, e.g., @Autowired private OrderService orderService;.

Container shutdown – Bean destruction

When the application context closes ( ApplicationContext.close()), beans that implement DisposableBean have destroy() called, and beans with a destroy‑method are also invoked.

Full lifecycle order

1 BeanDefinition load
2 Bean instantiation
3 Property injection
4 BeanNameAware
5 BeanFactoryAware
6 BeanPostProcessor before
7 InitializingBean
8 init‑method
9 BeanPostProcessor after
10 Bean ready
11 DisposableBean
12 destroy‑method

Complete example

Project structure /src/main/java/com/icoderoad/lifecycle contains a LifeCycleBean with @PostConstruct and @PreDestroy methods that print messages on initialization and destruction.

Understanding these ten stages reveals why Spring AOP works, how transactions are woven, how autowiring is performed, and where extension points reside, making the bean lifecycle essential knowledge for any advanced Spring engineer.

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.

Spring BootDependency InjectionBeanPostProcessorBean LifecycleInitializingBeanDisposableBean
LuTiao Programming
Written by

LuTiao Programming

LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.

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.