spring5.0 源码解析 registerDisposableBeanIfNecessary12

registerDisposableBeanIfNecessary

注册销毁bean时的回调

	protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
		AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);

		//判断 bean 不是原型 并且 有销毁方法
		if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
			// 如果是单例 bean 在bean工厂中注册销毁回调
			if (mbd.isSingleton()) {
				// Register a DisposableBean implementation that performs all destruction
				// work for the given bean: DestructionAwareBeanPostProcessors,
				// DisposableBean interface, custom destroy method.

				//注册执行所有销毁的DisposableBean实现
				//为给定bean工作:DestructionAwareBeanPostProcessors,
				//DisposableBean接口,自定义销毁方法。
				registerDisposableBean(beanName, new DisposableBeanAdapter(
						bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
			}
			else {
				// A bean with a custom scope...
				// 如果不是单例的 注册回调到他们的各自的 scope
				Scope scope = this.scopes.get(mbd.getScope());
				if (scope == null) {
					throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
				}
				scope.registerDestructionCallback(beanName, new DisposableBeanAdapter(
						bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
			}
		}
	}

DisposableBeanAdapter

适配器模式,为销毁的bean创建一个适配器

public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition,
			List<DestructionAwareBeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) {

		Assert.notNull(bean, "Disposable bean must not be null");
		this.bean = bean;
		this.beanName = beanName;
		this.invokeDisposableBean =
				(this.bean instanceof DisposableBean && !beanDefinition.isExternallyManagedDestroyMethod("destroy"));
		this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed();
		this.acc = acc;
		// 对destroyMethodName进行推断
		String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition);

		// 1、推断结果不为空
		// 2、invokeDisposableBean为false,或者destroyMethodName = destroy
		// 3、保存的回调方法名不存在 destroyMethodName
		if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) &&
				!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) {
			this.destroyMethodName = destroyMethodName;
			// 通过destroyMethodName获取到对应的method
			Method destroyMethod = determineDestroyMethod(destroyMethodName);
			if (destroyMethod == null) {
				if (beanDefinition.isEnforceDestroyMethod()) {
					throw new BeanDefinitionValidationException("Could not find a destroy method named '" +
							destroyMethodName + "' on bean with name '" + beanName + "'");
				}
			}
			else {
				if (destroyMethod.getParameterCount() > 0) {
					Class<?>[] paramTypes = destroyMethod.getParameterTypes();
					// 销毁方法参数个数不能多于1个
					if (paramTypes.length > 1) {
						throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
								beanName + "' has more than one parameter - not supported as destroy method");
					}
					// 销毁方法的唯一参数只能是boolean类型
					else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) {
						throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
								beanName + "' has a non-boolean parameter - not supported as destroy method");
					}
				}
				destroyMethod = ClassUtils.getInterfaceMethodIfPossible(destroyMethod);
			}
			this.destroyMethod = destroyMethod;
		}
		// 从postProcessors查找到所有适用于bean的销毁processors
		this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
	}

Initializing与 Disposable 执行顺序

关于在Spring 容器 初始化和销毁 bean 前所做的操作有三种方式定义:

  • 通过@PostConstruct 和 @PreDestroy 方法 实现初始化后和销毁bean之前进行的操作

  • 通过bean实现InitializingBean和 DisposableBean接口

  • 通过 在xml中配置init-method 和 destory-method方法,或者 配置@Bean(initMethod = “initMethod”, destroyMethod = “destroyMethod”) 注解

  • @PostConstruct -> InitializingBean -> 配置initMethod -> @PreDestroy -> DisposableBean -> 配置destroyMethod

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Security 是一个功能强大且广泛使用的安全框架,用于保护 Java 应用程序的身份验证和授权。它提供了一套全面的安全解决方案,包括认证、授权、攻击防护等功能。 Spring Security 的码是开的,可以从官方仓库(https://github.com/spring-projects/spring-security)获取到。在码中,主要包含以下几个关键模块: 1. 核心模块(Core):提供了基本的认证和授权功能,包括用户身份认证、访问控制等。核心模块的码位于 `spring-security-core` 包下。 2. Web 模块(Web):提供了与 Web 应用程序集成的相关功能,如基于 URL 的授权、Web 表单登录、记住我功能等。Web 模块的码位于 `spring-security-web` 包下。 3. 配置模块(Config):提供了基于 Java 配置和 XML 配置的方式来配置 Spring Security。配置模块的码位于 `spring-security-config` 包下。 4. 测试模块(Test):提供了用于测试 Spring Security 的工具和辅助类。测试模块的码位于 `spring-security-test` 包下。 在码中,你可以深入了解 Spring Security 的内部工作原理、各个组件之间的协作关系以及具体的实现细节。可以通过跟踪调试码,了解每个功能是如何实现的,从而更好地理解和使用 Spring Security。 请注意,Spring Security 的码是非常庞大且复杂的,需要一定的时间和精力去深入研究。建议在阅读码之前,先对 Spring Security 的基本概念和使用方法有一定的了解,这样会更有助于理解码中的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1999

每人一点点,明天会更好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值