diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java index b6bda429498..1f944245d57 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java @@ -168,11 +168,21 @@ public @interface Bean { * The optional name of a method to call on the bean instance upon closing the * application context, for example a {@code close()} method on a {@code DataSource}. * 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 based on the return type of the {@code @Bean} method. For example, given a + * {@code @Bean} method returning an Apache Commons DBCP {@code BasicDataSource}, the + * container will notice the {@code close()} method available on that type and + * automatically register it as the {@code destroyMethod}. By contrast, for a return + * type of JDBC {@code DataSource} interface (which does not declare a {@code close()} + * method, no inference is possible and the user must fall back to manually declaring + * {@code @Bean(destroyMethod="close")}. + *
To disable destroy method inference for a particular {@code @Bean}, specify an + * empty string as the value, e.g. {@code @Bean(destroyMethod="")}. *
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. * @see org.springframework.context.ConfigurableApplicationContext#close() */ - String destroyMethod() default ""; + String destroyMethod() default ConfigurationClassUtils.INFER_METHOD; } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java index 2d192a809c9..99ebab95af3 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java @@ -46,6 +46,8 @@ abstract class ConfigurationClassUtils { private static final String CONFIGURATION_CLASS_ATTRIBUTE = Conventions.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass"); + static final String INFER_METHOD = ""; // TODO SPR-8751 update to '-' or some such + /** * Check whether the given bean definition is a candidate for a configuration class,