Bean lifecycle
24 questions in 7 subtopics.
Lifecycle phases
List the steps Spring goes through to create a singleton bean, from instantiation to ready for use, and the steps at shutdown.A@PostConstruct method calls a @Transactional method on the same bean and no transaction is started. Explain using the lifecycle.A bean's constructor calls a method on an injected dependency that has not finished its own initialisation, causing a NullPointerException at startup. How do you diagnose and fix initialisation order problems?Spring offers several ways to move startup work out of the runtime: AOT processing, class data sharing and checkpoint/restore. How would you decide which, if any, your company should adopt for its services?Spring's lifecycle model was designed for long-running application servers. With containers, autoscaling and serverless, what changes in how you design startup and shutdown?@PostConstruct and @PreDestroy
What are@PostConstruct and @PreDestroy, where do they come from, and when is @PreDestroy not called?A service opens a connection pool in @PostConstruct and closes it in @PreDestroy. During Kubernetes rolling updates, requests fail with closed-pool errors. What is happening and how do you fix it?Many services warm caches and call remote services in @PostConstruct, and startup in Kubernetes is slow and fails when dependencies are down. What standard would you set?InitializingBean and DisposableBean
CompareInitializingBean and DisposableBean with @PostConstruct, @PreDestroy and @Bean(initMethod, destroyMethod). In what order do they run if all are present?A third-party client bean defined with @Bean has a public close() method, and the team sees it being closed at shutdown even though they never configured that. Why, and when is it a problem?BeanPostProcessor
What is aBeanPostProcessor, and give three examples Spring uses internally.Write a BeanPostProcessor that wraps every bean annotated with a custom @Timed annotation in a proxy that logs method time.Spring logs 'Bean X is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)'. What does it mean and why does it matter?A platform team wants a BeanPostProcessor in the shared starter that wraps every HTTP client bean to add metrics and tracing. Evaluate the design and alternatives.BeanFactoryPostProcessor
What is the difference between aBeanFactoryPostProcessor and a BeanPostProcessor?How does ConfigurationClassPostProcessor turn @Configuration, @ComponentScan and @Import into bean definitions?An internal starter needs to adjust bean definitions contributed by other auto-configurations, for example marking certain beans lazy. Would you use a BeanFactoryPostProcessor? What are the risks?An old in-house framework built on Spring uses dozens of BeanFactoryPostProcessors to rewrite bean definitions at startup. It blocks upgrades and AOT. How would you plan its retirement?Aware interfaces
What are Spring's Aware interfaces, and why are they used less in application code today?A codebase has a staticApplicationContextHolder set through ApplicationContextAware, used to call getBean from entities and utilities. What problems does this cause and how would you remove it?SmartLifecycle and phases
What isSmartLifecycle, and how do phases control start and stop order?You have a Kafka consumer, an HTTP server and a scheduled job runner in one service. Design start and stop ordering using SmartLifecycle so no work is lost at shutdown.Your services must support CRaC checkpoint and restore or fast scale-to-zero. How does the Spring lifecycle model help, and what must service code do?Frequent deployments and autoscaling mean your services start and stop thousands of times a day. How do you set standards for startup and shutdown behaviour across the company and keep them enforced?No question matches these choices.