Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

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?

For a @Bean, the destroyMethod attribute is inferred by default: at shutdown Spring looks for a public no-argument method named close or shutdown on the returned object and calls it, and beans that implement AutoCloseable are closed in the same way. That is why the client is closed although nobody configured it. It is a problem when the object is shared and owned elsewhere, for example a client provided by another library or a JNDI resource, and we turn the inference off with @Bean(destroyMethod = "").

Why this question is asked

Most of the time destroy method inference is a quiet helper: connection pools, executors and HTTP clients are closed at shutdown without anybody writing a line of code. Teams usually discover it only when it closes something it should not, often a shared client that another component, a parent context or a library still uses. Interviewers ask this to see whether we know the rule, can explain where it comes from, and can judge when it helps and when it hurts. In this answer, we will reproduce what the team saw, explain the inference rule and its link to AutoCloseable, look at the cases where it causes failures, and see how to switch it off and how to check what Spring will call.

1Reproducing What the Team Saw

The team wraps a third-party client in a @Bean method and never mentions any lifecycle method.

Java
@Configuration
class SearchConfig {
@Bean
SearchClient searchClient(SearchProperties p) {
return SearchClient.builder().endpoint(p.endpoint()).build();
}
}
// The library's class
public class SearchClient {
public List<Hit> query(String q) { /* ... */ return List.of(); }
public void close() { System.out.println("SearchClient closed"); } // public, no arguments
}
// On application shutdown (Ctrl+C or SIGTERM):
// SearchClient closed

Nothing in our configuration says 'close'. Yet the line appears every time the application stops. The behaviour is documented and deliberate, but it is easy to miss because it is a default rather than something we write.

2The Inference Rule

Every @Bean annotation has a destroyMethod attribute. Its default value is not an empty string but a special marker, "(inferred)", defined as AbstractBeanDefinition.INFER_METHOD. When the context is closed and Spring destroys the bean, it sees the marker and looks at the bean's class: if there is a public method named close with no arguments, it calls that; otherwise, if there is a public no-argument method named shutdown, it calls that. If neither exists, nothing happens.

The reason for this default is practical. A large share of the objects we create in @Bean methods hold resources: connection pools, thread pools, HTTP clients, message producers. Almost all of them have a close() or shutdown() method. If Spring did not call it, every developer would have to remember destroyMethod = "close", and many would forget, leaving threads and connections open until the JVM exits. Inference turns the common correct behaviour into the default.

Step throughWhat Spring does when it destroys a @Bean
  1. destroyMethod set explicitly?
    YesCall that method (empty string: call nothing)NoValue is (inferred)
  2. Public no-arg close() on the class?
    YesCall close()NoLook for shutdown()
  3. Public no-arg shutdown()?
    YesCall shutdown()NoNo destroy method

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

DisposableBean.destroy() and @PreDestroy still run before this step if present.

3AutoCloseable Beans Are Closed Too

The same idea applies beyond @Bean methods. If a bean's class implements AutoCloseable, which includes Closeable, Spring treats close() as its destroy method even when the bean comes from component scanning or when no destroy method was declared. This is a natural extension: AutoCloseable is Java's own way of saying 'this object must be closed when we are done with it'.

It means the rule covers more objects than people expect. Since Java 21, java.net.http.HttpClient implements AutoCloseable, and ExecutorService gained a close() method in Java 19, so a @Bean returning either of them will be closed at shutdown. Usually that is exactly right.

4When It Is a Problem

Inference assumes that the bean definition owns the object it returns. That is true when our @Bean method creates the object. It is false when the method merely exposes an object that somebody else created and manages.

Java
@Bean
HttpClient sharedHttpClient() {
return PartnerSdk.httpClient(); // the SDK's own static client, used by the SDK internally
}
// At shutdown Spring calls close() on it. Any SDK call still running in another thread:
// java.io.IOException: closed (or the SDK fails with its own error)
@Bean
DataSource legacyDs() throws NamingException {
return (DataSource) new InitialContext().lookup("java:comp/env/jdbc/legacy"); // owned by the app server
}

In the first case the SDK keeps its own client and uses it for background work, such as token refresh. Spring closes it during shutdown while the SDK may still need it, and the errors appear in the shutdown logs or in another application context that shares the same SDK. In the second case, the data source belongs to the application server, and closing it from our application can affect other applications on the same server.

Parent and child contexts are a third case. If a child context exposes a bean that is really owned by the parent, closing the child, for example at the end of a test or when a plugin is unloaded, closes the parent's object as well. In every case the rule is the same: the owner closes, and anyone else must not.

5Turning Inference Off and Checking It

To tell Spring that it does not own the object, we set destroyMethod to an empty string. Spring then calls nothing for that bean, whatever methods its class has.

Java
@Bean(destroyMethod = "") // we do not own it: never close
HttpClient sharedHttpClient() { return PartnerSdk.httpClient(); }
@Bean(destroyMethod = "")
DataSource legacyDs() throws NamingException { /* JNDI lookup */ }
// Checking what Spring will call, for example in a test:
BeanDefinition bd = ctx.getBeanFactory().getBeanDefinition("sharedHttpClient");
System.out.println("[" + bd.getDestroyMethodName() + "]");
// [] (with destroyMethod = ""; the default would print [(inferred)])

Notice the opposite case too. If the method has a different name, for example release(), inference will not find it, and we must name it explicitly with destroyMethod = "release". In XML configuration, inference is off by default and must be asked for with destroy-method="(inferred)", which is another reason teams moving from XML to @Bean are surprised.

6Follow-up questions

Does inference also apply to init methods?

No. There is no inferred init method; Spring only calls an init method that is named with initMethod, or afterPropertiesSet() and @PostConstruct. Inference exists only for destruction, because closing resources is the common case that people forget.

What if the bean has both a @PreDestroy method and a close() method?

Both run, @PreDestroy first and then the inferred close(), unless they are the same method, in which case Spring calls it once. If @PreDestroy already releases everything, a second close() on most well-written classes is harmless because close() should be idempotent, but it is worth checking for third-party classes.

Is it a problem for a bean with prototype scope?

Prototype beans are never destroyed by the container, so no destroy method, inferred or explicit, is ever called on them. Whoever obtained the prototype must close it.

7Conclusion

In this answer, we saw that a @Bean's destroyMethod defaults to "(inferred)", so Spring calls a public no-argument close() or shutdown() at shutdown, and that AutoCloseable beans are closed the same way. This is what closed the team's client. It is the right behaviour for objects the bean method creates and owns, and wrong for objects owned elsewhere, such as an SDK's shared client, a JNDI data source or a bean owned by a parent context. We turn it off with @Bean(destroyMethod = "") and can check the result in the bean definition.

What to say in the interviewAbout 80 seconds aloud

Open with

This is destroy method inference. The destroyMethod attribute on @Bean does not default to nothing; it defaults to a marker, (inferred). When the context closes, Spring looks for a public no-argument close method on the returned object and calls it, or failing that a public shutdown method.

Then, if they want more

Separately, any bean that implements AutoCloseable gets its close method called, even from component scanning. So because the client class has a public close, Spring closes it at shutdown without anyone configuring it. Most of the time that is exactly what we want, because pools, executors and HTTP clients get cleaned up automatically.

It becomes a problem when the bean method does not own the object, for example when it just exposes a client that a library created and still uses internally, a DataSource looked up from JNDI that the application server owns, or a bean that really belongs to a parent context.

Then Spring closes something others still use, and we see errors during shutdown or in another context. The fix is @Bean(destroyMethod = ""), which tells Spring to call nothing. And the opposite case is a method with a different name, like release, which I have to name explicitly.

About this answer

Last updated
12 November 2026
Versions it names
Java 19, Java 21
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 "A third-party client bean defined with @Bean has a public close() method, and the team...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/71f9a8af8e/