TLDR;
Spring tries to automatically infer a destroyMethod
when using Java configuration (but it does not do so when using XML configuration). To disable this automatic inference, use:
@Bean(destroyMethod="")
The answer is in the JavaDoc of the @Bean
annotation; specifically on the org.springframework.context.annotation.Bean.destroyMethod()
method (emphasis mine):
The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object. The method must have no arguments but may throw any exception.
As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @Bean method. For example, given a @Bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close'. The method may be declared at any level of the inheritance hierarchy and will be detected regardless of the return type of the @Bean method (i.e., detection occurs reflectively against the bean instance itself at creation time).
To disable destroy method inference for a particular @Bean, specify an empty string as the value, e.g. @Bean(destroyMethod=""). Note that the org.springframework.beans.factory.DisposableBean and the java.io.Closeable/java.lang.AutoCloseable interfaces will nevertheless get detected and the corresponding destroy/close method invoked.
Note: Only invoked on beans whose lifecycle is under the full control of the factory, which is always the case for singletons but not guaranteed for any other scope.
After changing the Java configuration to:
@Bean(destroyMethod="")
public ItemReader<MyTable> cetFileReader(EntityManagerFactory entityManagerFactory) {
JpaPagingItemReader<MyTable> itemReader = new JpaPagingItemReader<MyTable>();
itemReader.setEntityManagerFactory(entityManagerFactory);
itemReader.setQueryString("select mt from MyTable mt where status in ('1','2','3')");
itemReader.setPageSize(1000);
return itemReader;
}
The warning did not show up anymore. I was able to confirm this by placing a breakpoint on the org.springframework.beans.factory.support.DisposableBeanAdapter.destroy()
method and launching XML configured job and the Java configured job.
For the XML configuration:
- DisposableBeanAdapter.invokeDisposableBean = false
- DisposableBeanAdapter.destroyMethod = null
- DisposableBeanAdapter.destroyMethodName = null
For the Java configuration (without destroyMethod=""
set):
- DisposableBeanAdapter.invokeDisposableBean = false
- DisposableBeanAdapter.destroyMethod = public void org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close() throws org.springframework.batch.item.ItemStreamException
- DisposableBeanAdapter.destroyMethodName = close
For the Java configuration (with destroyMethod=""
set):
- DisposableBeanAdapter.invokeDisposableBean = false
- DisposableBeanAdapter.destroyMethod = null
- DisposableBeanAdapter.destroyMethodName = null
Based on these observations, I come to the conclusion that the container does not try to infer a destroy method when configured via XML; but it does when configured via Java. Which is why the warning shows up for the Java configuration and not the XML configuration.
Additionally, the method the container infers is the destroyMethod seems to come from org.springframework.batch.item.ItemStreamSupport.close()
. So this could potentially happen to any bean that implements the ItemStreamSupport
interface that is configured via the @Bean
annotation.
A note has been added to the Spring Framework Reference material for @Bean describing this behavior:
By default, beans defined using Java config that have a public close or shutdown method are automatically enlisted with a destruction callback. If you have a public close or shutdown method and you do not wish for it to be called when the container shuts down, simply add @Bean(destroyMethod="") to your bean definition to disable the default (inferred) mode.