Spring Bean Life Cycle | PostConstruct and PreDestroy Spring Bean Life Cycle Interview Questions

Опубликовано: 23 Апрель 2026
на канале: Dip Developer
187
3

Understanding the Spring Bean Life Cycle | What is the life cycle of a bean in Spring?
Spring Bean Life Cycle | PostConstruct and PreDestroy Spring | Spring Bean Life Cycle Interview Questions

--------------------------------------------------------------------------------------------------------
🔗 Related Videos:
Spring Bean Scope:    • Spring Bean Scopes in Spring Boot | Single...  

Spring @Bean Annotation:    • Spring @Bean Annotation with Example | Wha...  

Spring @Component Annotation:    • Spring @Component Annotation Example | Com...  

@Autowired and @Qualifier Annotation:    • @Autowired and @Qualifier Annotation | Aut...  

Dependency Injection using Constructor Injection:    • Constructor Based Dependency Injection Exa...  

Spring IoC Container Explained:    • Spring IoC Tutorial | How Spring IoC Conta...  

Spring Framework Tutorial Playlist:    • Spring Framework Tutorial for Beginners | ...  
--------------------------------------------------------------------------------------------------------
📚 Topics Covered:
Spring Bean Life Cycle using @PostConstruct @PreDestroy [init - method] [destroy - method]
add jakarta.annotation-api dependency [@PostConstruct @PreDestroy]
add context:annotation-config tags to activate annotation
dbConnectionOpen method is annotated with @PostConstruct
once spring create the employee object , pls call dbConnectionOpen() method
Introducing init() method // used to setup the resources like db/socket/file/newconnecton etc
step 1: Object created inside container
step 2: dependency injected (values injected)
step 3: init method called (@PostConstruct)
step 4: bean/obj is ready for use
step 5:
now lets clean up the unused references
Introducing @PreDestroy annotation
destroy method is called before the bean is removed from container
for standalone application like this, we need to create and destroy/close container object manually
for web application like (spring boot), create and destroy/close container object done automatically
step 5: container shutdown or .closed()
step 6: before you destroy, your custom destroy method is called(@PreDestroy)
step 7:
Another way to activate lifecyle annotation
lets remove context:annotation-config tag and use CommonAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor (import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;)

Configure bean lifecycle with xml file
comment @PostConstruct and @PreDestroy annotation
introducing init-method="init" destroy-method="destroy"
those method is used to specify lifecycle methods in xml
another way to close the container .registerShutdownHook();
line number does not matter for .registerShutdownHook()
will execute once main() methods ends
default-init-method="" default-destroy-method="" if you have multiple classes with init and destroy method
coming up spring bean scope

--------------------------------------------------------------------------------------------------------
👉 Don't forget to SUBSCRIBE, LIKE, and SHARE this video if you find it helpful!
-----------------------------------------------------------------------------------------------------------------------------------
🔑 Your Search Queries:
What is the life cycle of a bean in Spring?
What are the 4 stages of the bean life cycle?
What is the lifetime of spring boot beans?
How does @bean work in Spring?
postconstruct annotation spring boot
postconstruct and predestroy spring
postconstruct annotation
postconstruct spring
postconstruct
@postconstruct and @predestroy
Spring bean life cycle in spring boot
Spring bean life cycle javatpoint
Spring bean life cycle diagram
Spring bean life cycle java
Spring bean life cycle baeldung
Spring bean life cycle Interview questions
Spring bean scopes
Spring bean life cycle example

spring bean life cycle
spring bean scopes
spring bean configuration file not found in sts
spring bean life cycle durgasoft
spring bean scopes in detail
spring bean life cycle interview questions
spring bean scopes selenium express
spring bean life cycle tamil
spring bean configuration xml file in eclipse
spring bean factory
spring bean life cycle java techie
spring bean life cycle in java
spring bean xml configuration
spring bean annotation

bean lifecycle spring
bean life cycle in spring
bean scopes in spring
bean spring boot
bean annotation in spring boot

bean lifecycle spring boot
--------------------------------------------------------------------------------------------------------
📌 Keywords:
the dip developer,subhadip mondal,spring bean life cycle annotations,spring bean life cycle interview questions,bean and life cycle in spring boot,postconstruct annotation spring boot,postconstruct and predestroy spring,postconstruct spring,@postconstruct and @predestroy,@predestroy annotation in spring,understanding the spring bean life cycle,registershutdownhook spring,registershutdownhook vs spring bean lifecycle,registershutdownhook method in spring,spring bean