Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are @PostConstruct and @PreDestroy, where do they come from, and when is @PreDestroy not called?

@PostConstruct and @PreDestroy are lifecycle annotations from Jakarta Annotations, in the jakarta.annotation package in Spring Boot 3 and later, which Spring processes with CommonAnnotationBeanPostProcessor. @PostConstruct marks a method that runs once after the bean's dependencies are injected, and @PreDestroy marks a method that runs when the container destroys the bean. @PreDestroy is not called for prototype beans, because Spring does not track them, and it is not called when the JVM is killed without its shutdown hooks running.

Why this question is asked

Almost every Spring application has a few beans that need to do something once they are ready, such as validate their settings or build a lookup table, and something when they go away, such as flush a buffer. @PostConstruct and @PreDestroy are the standard way to say that. Interviewers ask about them to check that we know where they come from, which matters after the move to Jakarta EE, and that we know their limits, because relying on @PreDestroy for important cleanup can lose data. In this answer, we will see what the annotations are and which package they live in, how Spring finds and calls them, and then the cases in which @PreDestroy never runs.

1What the Two Annotations Are

Both annotations come from a small Java specification called Common Annotations, originally JSR-250 and now Jakarta Annotations. They are not Spring annotations: the same annotations work in other containers, such as Jakarta EE application servers. Spring simply chose to support them, which is why they are a portable way to declare lifecycle methods.

A method annotated with @PostConstruct is called once, after the object is created and all of its dependencies are injected. That is the key difference from the constructor: in the constructor, fields filled by @Autowired are still null, while in a @PostConstruct method every injected field is set. A method annotated with @PreDestroy is called once, when the container is about to discard the bean, typically at application shutdown.

Both methods must take no arguments and must not be static. They can have any visibility, and by convention return void.

Java
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
@Component
class RateTable {
@Value("${rates.file}") private Path file; // field injection: null in the constructor
private Map<String, BigDecimal> rates;
@PostConstruct
void load() {
rates = RateFiles.read(file); // file is set here
System.out.println("loaded " + rates.size()); // loaded 42
}
@PreDestroy
void close() {
System.out.println("rate table released"); // printed at context shutdown
}
}

2Javax or jakarta: Which Package?

Until Java 8 these annotations were part of the JDK, in javax.annotation. Java 11 removed that module from the JDK, so they had to come from a library dependency instead. Then the Java EE specifications moved to the Eclipse Foundation as Jakarta EE, and from Jakarta EE 9 on, every javax package of those specifications was renamed to jakarta.

Spring Framework 6 and Spring Boot 3 are built on Jakarta EE, so the annotations to use are jakarta.annotation.PostConstruct and jakarta.annotation.PreDestroy, from the jakarta.annotation-api library that Spring Boot starters bring in. A common upgrade mistake is to leave an old javax.annotation import in the code. Depending on your Spring version and classpath, such a method may be silently ignored (check your version); the safe rule is to replace every javax.annotation lifecycle import with jakarta during the upgrade.

3How Spring Finds and Calls Them

Spring does not look for these annotations in its core bean factory. A BeanPostProcessor called CommonAnnotationBeanPostProcessor does it. A BeanPostProcessor is a special bean that sees every other bean before and after initialisation. This one is registered automatically by annotation-based contexts, including every Spring Boot application, and it also handles @Resource injection.

When a bean is created, Spring first calls the constructor, then injects fields and setters, then calls the Aware callbacks. Next, in postProcessBeforeInitialization, CommonAnnotationBeanPostProcessor finds the @PostConstruct methods (it scans the class once and caches the result) and calls them. Only after that does Spring call afterPropertiesSet() from InitializingBean and any custom init method. At shutdown, the same post-processor, acting as a destruction-aware post-processor, calls the @PreDestroy methods before DisposableBean.destroy() and any custom destroy method.

Step throughWhere the two annotations run
  1. Constructor and injectionfields, setters, Aware callbacks
  2. @PostConstructcalled by CommonAnnotationBeanPostProcessor
  3. afterPropertiesSet, custom init method
  4. Bean in usepossibly wrapped in an AOP proxy
  5. Context closesSIGTERM, ctx.close(), end of a test context
  6. @PreDestroythen destroy() and custom destroy method

6 steps in order. Play them, or step through them one at a time.

4When @PreDestroy Is Not Called: Prototype Beans

For a singleton, Spring keeps the instance and knows when to destroy it. For a prototype bean, a new instance is created each time it is requested, and Spring hands it over and forgets about it. It never calls @PreDestroy on prototypes, because it does not hold a reference to them.

Java
@Component
@Scope("prototype")
class ExportJob {
@PostConstruct void start() { System.out.println("job created"); } // printed for every getBean
@PreDestroy void finish() { System.out.println("job finished"); } // never printed
}
var a = ctx.getBean(ExportJob.class); // job created
var b = ctx.getBean(ExportJob.class); // job created
ctx.close(); // (nothing for a or b)

Notice that @PostConstruct still runs for each prototype, because creation goes through the normal lifecycle. Only destruction is skipped. If a prototype holds a resource, the code that asked for it must release it, for example by implementing AutoCloseable on the class and using try-with-resources.

5When @PreDestroy Is Not Called: The JVM Dies First

@PreDestroy runs when the application context closes. Spring Boot registers a JVM shutdown hook that closes the context when the JVM begins a normal shutdown, which happens on SIGTERM, on Ctrl+C or when System.exit() is called. So in a container, a normal stop gives our @PreDestroy methods a chance to run.

But some ways of ending a process never run shutdown hooks. kill -9 (SIGKILL), a container killed by the out-of-memory killer, a JVM crash or a power loss end the process immediately, and nothing in Java gets a chance to run. In Kubernetes, if the application takes longer than terminationGracePeriodSeconds to shut down, it is killed with SIGKILL, and remaining destroy methods are lost. In a plain Spring application outside Spring Boot, the context is not closed at all unless we call close() or registerShutdownHook() ourselves.

So @PreDestroy is a good place for best-effort cleanup, such as closing clients or flushing metrics. Anything that must not be lost, like an unsent payment instruction, should be written durably when it happens, not held in memory until shutdown.

6Follow-up questions

Can a bean have more than one @PostConstruct method?

Within one class, the specification expects a single method, but Spring calls the @PostConstruct methods of a class and its superclasses, superclass methods first. In practice we keep one method per class to keep the order obvious.

What happens if a @PostConstruct method throws an exception?

Bean creation fails, Spring wraps the error in a BeanCreationException, and for a singleton the whole context fails to start. That is often what we want for a validation, but it is a reason to keep slow or remote calls out of @PostConstruct.

Do the annotations work on a @Bean method's returned object?

Yes. The returned object is still processed by CommonAnnotationBeanPostProcessor, so its @PostConstruct and @PreDestroy methods run. For classes we cannot annotate, @Bean(initMethod = ..., destroyMethod = ...) does the same job.

7Conclusion

In this answer, we saw that @PostConstruct and @PreDestroy are Jakarta Annotations, found in jakarta.annotation since Spring Boot 3, and processed by Spring's CommonAnnotationBeanPostProcessor. @PostConstruct runs after injection and before afterPropertiesSet, and @PreDestroy runs when the container destroys the bean, before DisposableBean.destroy(). We also saw the two cases in which @PreDestroy never runs: prototype beans, which Spring does not track, and a JVM that is killed before its shutdown hooks run.

What to say in the interviewAbout 80 seconds aloud

Open with

@PostConstruct and @PreDestroy come from the Common Annotations specification, JSR-250, which is now Jakarta Annotations. They used to be in javax.annotation inside the JDK, but that was removed in Java 11, and since Spring Boot 3 and Spring Framework 6 we use jakarta.annotation from the jakarta.annotation-api dependency.

Then, if they want more

A leftover javax import is a typical upgrade bug. Spring handles them with CommonAnnotationBeanPostProcessor, which is registered automatically. @PostConstruct runs once after the constructor and after all injection, in the before-initialisation step, so injected fields are available there, and it runs before afterPropertiesSet and any custom init method. @PreDestroy runs when the context destroys the bean, before DisposableBean.destroy.

There are two important cases where @PreDestroy is not called. Prototype beans never get it, because Spring hands them out and does not track them, so the caller must clean them up.

And it only runs when the context is closed, which Spring Boot does from a shutdown hook on a normal JVM exit, so with kill -9, an out-of-memory kill, a crash, or exceeding the Kubernetes grace period, it never runs. So I use it for best-effort cleanup, not for anything that must not be lost.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Jakarta EE 9, Java 11, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are @PostConstruct and @PreDestroy, where do they come from, and when is...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/afd4f96d87/