Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'destroy method inference' for @Bean methods [SPR-8751] #13393

Closed
spring-projects-issues opened this issue Oct 8, 2011 · 2 comments
Closed
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 8, 2011

Chris Beams opened SPR-8751 and commented

Use case: creating a Hibernate SessionFactory using the native Hibernate API or Spring 3.1's LocalSessionFactoryBuilder:

@Bean
public SessionFactory sessionFactory() {
    // ...
    return sessionFactory;
}

In order to properly destroy the SessionFactory on Spring container shutdown, the user must specify @Bean(destroyMethod="close"):

@Bean(destroyMethod="close")
public SessionFactory sessionFactory() {
    // ...
    return sessionFactory;
}

This is easy to forget and should be specified every time anyway. "destroy method inference" will detect well-known destroy methods (e.g. public no-arg close() methods) and automatically register them as the destroy-method against the underlying bean definition.

Users may disable destroy method inference by explicitly specifying empty string ("") for the value of destroyMethod:

@Bean(destroyMethod="") // call no destroy method and do not attempt to infer one
public SessionFactory sessionFactory() {
    // ...
    return sessionFactory;
}

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

commit a058eac6d55950d9b0979b2e0be85216f3bb727b (HEAD, master)
Author: Chris Beams <cbeams@vmware.com>
Date:   Sat Oct 8 23:26:58 2011 -0700

    Add INFER_METHOD constant and update @Bean Javadoc
    
    In anticipation of 'destroy method inference' feature, introduce
    ConfigurationClassUtils#INFER_METHOD and update @Bean#destroyMethod to
    reflect its use.
    
    Issue: SPR-8751

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

commit 2880d176109e739066178865e7571313b65595e1
Author: Chris Beams <cbeams@vmware.com>
Date:   Wed Oct 12 02:09:04 2011 +0000

    Support destroy method inference
    
    Anywhere the value of a destroy method may be expressed, specifying
    the value "(inferred)" now indicates that the container should attempt
    to automatically discover a destroy method. This functionality is
    currently limited to detecting public, no-arg methods named 'close';
    this is particularly useful for commonly used types such as Hibernate
    SessionFactory most JDBC DataSource implementations, JMS connection
    factories, and so forth.
    
    This special value is captured as the constant
    AbstractBeanDefinition#INFER_METHOD, which in turn serves as the default
    value of the @Bean#destroyMethod attribute.
    
    For example in the following case
    
        @Bean
        public BasicDataSource dataSource() { ... }
    
    the container will automatically detect BasicDataSource#close and invoke
    it when the enclosing ApplicationContext is closed. This is exactly
    equivalent to
    
        @Bean(destroyMethod="(inferred)")
        public BasicDataSource dataSource() { ... }
    
    A user may override this inference-by-default convention simply by
    specifying a different method
    
        @Bean(destroyMethod="myClose")
        public MyBasicDataSource dataSource() { ... }
    
    or, in the case of a bean that has an otherwise inferrable 'close'
    method, but the user wishes to disable handling it entirely, an empty
    string may be specified
    
        @Bean(destroyMethod="")
        public MyBasicDataSource dataSource() { ... }
    
    The special destroy method name "(inferred)" may also be specified in
    an XML context, e.g.
    
        <bean destroy-method="(inferred)">
            or
        <beans default-destroy-method="(inferred)">
    
    Note that "(inferred)" is the default value for @Bean#destroyMethod,
    but NOT for the destroy-method and default-destroy-method attributes
    in the spring-beans XML schema.
    
    The principal reason for introducing this feature is to avoid forcing
    @Configuration class users to type destroyMethod="close" every time a
    closeable bean is configured. This kind of boilerplate is easily
    forgotten, and this simple convention means the right thing is done
    by default, while allowing the user full control over customization or
    disablement in special cases.
    
    Issue: SPR-8751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant