1. // Register bean as disposable.
    2. try {
    3. // 注册销毁的方法
    4. registerDisposableBeanIfNecessary(beanName, bean, mbd);
    5. } catch (BeanDefinitionValidationException ex) {
    6. throw new BeanCreationException(
    7. mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    8. }
    1. protected void registerDisposableBeanIfNecessary(String beanName, Object bean
    2. , RootBeanDefinition mbd) {
    3. AccessControlContext acc
    4. = (System.getSecurityManager() != null ? getAccessControlContext() : null);
    5. if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
    6. if (mbd.isSingleton()) {
    7. // Register a DisposableBean implementation that performs all destruction
    8. // work for the given bean: DestructionAwareBeanPostProcessors,
    9. // DisposableBean interface, custom destroy method.
    10. registerDisposableBean(beanName,
    11. new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
    12. } else {
    13. // A bean with a custom scope...
    14. Scope scope = this.scopes.get(mbd.getScope());
    15. if (scope == null) {
    16. throw new IllegalStateException("No Scope registered for scope name '"
    17. + mbd.getScope() + "'");
    18. }
    19. scope.registerDestructionCallback(beanName,
    20. new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
    21. }
    22. }
    23. }

    Spring 中不但提供了对于初始化方法的扩展入口,同样也提供了销毁方法的扩展入口,对于销毁方法的扩展,除了我们熟知的配置属性 destroy-method 方法外,用户还可以注册后处理器 DestructionAwareBeanPostProcessor 来统一处理 bean 的销毁方法。