Scrivio Interview library

Bean lifecycle

24 questions in 7 subtopics.

Lifecycle phases

InternalsFoundations6 min read
List the steps Spring goes through to create a singleton bean, from instantiation to ready for use, and the steps at shutdown.
DebuggingFoundations6 min read
A @PostConstruct method calls a @Transactional method on the same bean and no transaction is started. Explain using the lifecycle.
DebuggingIntermediate7 min read
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?
ScenarioPrincipal7 min read
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?
LeadershipPrincipal7 min read
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

ConceptFoundations6 min read
What are @PostConstruct and @PreDestroy, where do they come from, and when is @PreDestroy not called?
ScenarioIntermediate6 min read
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?
ScenarioAdvanced7 min read
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

ConceptFoundations6 min read
Compare InitializingBean and DisposableBean with @PostConstruct, @PreDestroy and @Bean(initMethod, destroyMethod). In what order do they run if all are present?
DebuggingIntermediate6 min read
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

ConceptFoundations6 min read
What is a BeanPostProcessor, and give three examples Spring uses internally.
CodingIntermediate7 min read
Write a BeanPostProcessor that wraps every bean annotated with a custom @Timed annotation in a proxy that logs method time.
DebuggingIntermediate7 min read
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?
DesignAdvanced7 min read
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

ConceptFoundations5 min read
What is the difference between a BeanFactoryPostProcessor and a BeanPostProcessor?
InternalsIntermediate7 min read
How does ConfigurationClassPostProcessor turn @Configuration, @ComponentScan and @Import into bean definitions?
DesignAdvanced6 min read
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?
ScenarioPrincipal7 min read
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

ConceptFoundations6 min read
What are Spring's Aware interfaces, and why are they used less in application code today?
ScenarioIntermediate6 min read
A codebase has a static ApplicationContextHolder 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

ConceptFoundations6 min read
What is SmartLifecycle, and how do phases control start and stop order?
DesignIntermediate7 min read
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.
ScenarioAdvanced7 min read
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?
LeadershipPrincipal6 min read
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?