Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions