当前位置: 首页 > news >正文

刚做网站和搜出来的不一样优化落实疫情防控

刚做网站和搜出来的不一样,优化落实疫情防控,东莞网站建设-信科网络,玉溪做网站bean销毁(找到销毁的bean) 在bean的声明周期中,存在一个记录bean销毁方法的阶段,以备于spring关闭的时候可以执行bean的销毁方法(单例bean) v1.0 registerDisposableBeanIfNecessary protected void registerDisposableBeanIfNec…

bean销毁(找到销毁的bean)

在bean的声明周期中,存在一个记录bean销毁方法的阶段,以备于spring关闭的时候可以执行bean的销毁方法(单例bean)

v1.0 registerDisposableBeanIfNecessary
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);//当rootBeanDefinition不是原型bean,并且requiresDestruction方法返回true代表当前bean是需要销毁的if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {if (mbd.isSingleton()) {//最终通过适配器模式将销毁方法存入disposableBeans这个Map中registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));}else {// A bean with a custom 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));}}}

v1.1 requiresDestruction
protected boolean requiresDestruction(Object bean, RootBeanDefinition mbd) {//首先bean不能为NULLBEAN,然后(实现了DisposableBean接口或实现了AutoCloseable) 或者有销毁的方法return (bean.getClass() != NullBean.class && (DisposableBeanAdapter.hasDestroyMethod(bean, mbd) ||(hasDestructionAwareBeanPostProcessors() && DisposableBeanAdapter.hasApplicableProcessors(bean, getBeanPostProcessorCache().destructionAware))));}

v1.2 hasDestroyMethod
public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {//如果实现了DisposableBean接口或实现了AutoCloseable,直接返回trueif (bean instanceof DisposableBean || bean instanceof AutoCloseable) {return true;}return inferDestroyMethodIfNecessary(bean, beanDefinition) != null;}private static String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {//从缓存中获取当前RootBeanDefinition的结束方法String destroyMethodName = beanDefinition.resolvedDestroyMethodName;if (destroyMethodName == null) {//如果缓存中没有,则尝试直接获取销毁方法名称destroyMethodName = beanDefinition.getDestroyMethodName(); ////判断bean销毁的名字是否等于(inferred)或者实现了AutoCloseable接口if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||(destroyMethodName == null && bean instanceof AutoCloseable)) {// Only perform destroy method inference or Closeable detection// in case of the bean not explicitly implementing DisposableBeandestroyMethodName = null;//进入这一阶段,spring会自己去寻找销毁方法if (!(bean instanceof DisposableBean)) {try {//获取类中的close方法destroyMethodName = bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();}catch (NoSuchMethodException ex) {try {//获取类中的SHUTDOWN_METHOD_NAME方法destroyMethodName = bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();}catch (NoSuchMethodException ex2) {// no candidate destroy method found}}}}//将销毁方法名字缓存到resolvedDestroyMethodName字段中beanDefinition.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");}return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);}
v1.3 hasDestructionAwareBeanPostProcessors
//判断destructionAware缓存是否为空
protected boolean hasDestructionAwareBeanPostProcessors() {return !getBeanPostProcessorCache().destructionAware.isEmpty();}BeanPostProcessorCache getBeanPostProcessorCache() {//先从缓存中拿取 BeanPostProcessorCache ,如果没获取到,说明还没有对beanPostProcessors进行分类BeanPostProcessorCache bpCache = this.beanPostProcessorCache;if (bpCache == null) {bpCache = new BeanPostProcessorCache();//对所有的beanPostProcessors进行循环分类for (BeanPostProcessor bp : this.beanPostProcessors) {if (bp instanceof InstantiationAwareBeanPostProcessor) {bpCache.instantiationAware.add((InstantiationAwareBeanPostProcessor) bp);if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {bpCache.smartInstantiationAware.add((SmartInstantiationAwareBeanPostProcessor) bp);}}if (bp instanceof DestructionAwareBeanPostProcessor) {bpCache.destructionAware.add((DestructionAwareBeanPostProcessor) bp);}if (bp instanceof MergedBeanDefinitionPostProcessor) {bpCache.mergedDefinition.add((MergedBeanDefinitionPostProcessor) bp);}}this.beanPostProcessorCache = bpCache;}return bpCache;}
v1.4 DisposableBeanAdahasApplicableProcessorspter.
	public static boolean hasApplicableProcessors(Object bean, List<DestructionAwareBeanPostProcessor> postProcessors) {if (!CollectionUtils.isEmpty(postProcessors)) {for (DestructionAwareBeanPostProcessor processor : postProcessors) {if (processor.requiresDestruction(bean)) {return true;}}}return false;}public boolean requiresDestruction(Object bean) {return findLifecycleMetadata(bean.getClass()).hasDestroyMethods();}private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {if (this.lifecycleMetadataCache == null) {// Happens after deserialization, during destruction...return buildLifecycleMetadata(clazz);}// Quick check on the concurrent map first, with minimal locking.LifecycleMetadata metadata = this.lifecycleMetadataCache.get(clazz);if (metadata == null) {synchronized (this.lifecycleMetadataCache) {metadata = this.lifecycleMetadataCache.get(clazz);if (metadata == null) {metadata = buildLifecycleMetadata(clazz);this.lifecycleMetadataCache.put(clazz, metadata);}return metadata;}}return metadata;}private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {return this.emptyLifecycleMetadata;}//此处使用了双层循环处理//该集合定义了所有的初始化方法List<LifecycleElement> initMethods = new ArrayList<>();//该集合中定义了所有的销毁方法List<LifecycleElement> destroyMethods = new ArrayList<>();Class<?> targetClass = clazz;do {final List<LifecycleElement> currInitMethods = new ArrayList<>();final List<LifecycleElement> currDestroyMethods = new ArrayList<>();ReflectionUtils.doWithLocalMethods(targetClass, method -> {//判断方法是否实现了@PostConstruct注解if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {LifecycleElement element = new LifecycleElement(method);currInitMethods.add(element);if (logger.isTraceEnabled()) {logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);}}//判断方法是否实现了@PreDestroy注解if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {currDestroyMethods.add(new LifecycleElement(method));if (logger.isTraceEnabled()) {logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);}}});//将父类的初始化方法放在最前面initMethods.addAll(0, currInitMethods);//将父类的销毁方法放在最后面destroyMethods.addAll(currDestroyMethods);//获取父类targetClass = targetClass.getSuperclass();}while (targetClass != null && targetClass != Object.class);return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata :new LifecycleMetadata(clazz, initMethods, destroyMethods));}

执行bean销毁

v1.1 destroySingletons
//销毁单例beanpublic void destroySingletons() {if (logger.isTraceEnabled()) {logger.trace("Destroying singletons in " + this);}synchronized (this.singletonObjects) {this.singletonsCurrentlyInDestruction = true;}String[] disposableBeanNames;synchronized (this.disposableBeans) {//获取所有有销毁方法的bean的名字disposableBeanNames = StringUtils.toStringArray(this.disposableBeans.keySet());}//遍历销毁所有的方法for (int i = disposableBeanNames.length - 1; i >= 0; i--) {destroySingleton(disposableBeanNames[i]);}this.containedBeanMap.clear();this.dependentBeanMap.clear();this.dependenciesForBeanMap.clear();clearSingletonCache();}public void destroySingleton(String beanName) {// Remove a registered singleton of the given name, if any.// 先从单例池中移除掉removeSingleton(beanName);// Destroy the corresponding DisposableBean instance.DisposableBean disposableBean;synchronized (this.disposableBeans) {disposableBean = (DisposableBean) this.disposableBeans.remove(beanName);}destroyBean(beanName, disposableBean);}

v1.1 destroyBean
protected void destroyBean(String beanName, @Nullable DisposableBean bean) {// dependentBeanMap表示某bean被哪些bean依赖了// 所以现在要销毁某个bean时,如果这个Bean还被其他Bean依赖了,那么也得销毁其他Bean// Trigger destruction of dependent beans first...Set<String> dependencies;synchronized (this.dependentBeanMap) {// Within full synchronization in order to guarantee a disconnected Setdependencies = this.dependentBeanMap.remove(beanName);}if (dependencies != null) {if (logger.isTraceEnabled()) {logger.trace("Retrieved dependent beans for bean '" + beanName + "': " + dependencies);}for (String dependentBeanName : dependencies) {destroySingleton(dependentBeanName);}}// Actually destroy the bean now...if (bean != null) {try {bean.destroy();}catch (Throwable ex) {if (logger.isWarnEnabled()) {logger.warn("Destruction of bean with name '" + beanName + "' threw an exception", ex);}}}// Trigger destruction of contained beans...Set<String> containedBeans;synchronized (this.containedBeanMap) {// Within full synchronization in order to guarantee a disconnected SetcontainedBeans = this.containedBeanMap.remove(beanName);}if (containedBeans != null) {for (String containedBeanName : containedBeans) {destroySingleton(containedBeanName);}}// Remove destroyed bean from other beans' dependencies.synchronized (this.dependentBeanMap) {for (Iterator<Map.Entry<String, Set<String>>> it = this.dependentBeanMap.entrySet().iterator(); it.hasNext();) {Map.Entry<String, Set<String>> entry = it.next();Set<String> dependenciesToClean = entry.getValue();dependenciesToClean.remove(beanName);if (dependenciesToClean.isEmpty()) {it.remove();}}}// Remove destroyed bean's prepared dependency information.this.dependenciesForBeanMap.remove(beanName);}


文章转载自:
http://wanjiatelecontrol.gcqs.cn
http://wanjiascrotal.gcqs.cn
http://wanjialucy.gcqs.cn
http://wanjiaformulation.gcqs.cn
http://wanjiaelectrovalency.gcqs.cn
http://wanjiahottish.gcqs.cn
http://wanjiaminatory.gcqs.cn
http://wanjiapmpo.gcqs.cn
http://wanjiaovum.gcqs.cn
http://wanjiadahomean.gcqs.cn
http://wanjiapsychotropic.gcqs.cn
http://wanjianeurohypophyseal.gcqs.cn
http://wanjiafrostwork.gcqs.cn
http://wanjiathermae.gcqs.cn
http://wanjiagusset.gcqs.cn
http://wanjiazonerefine.gcqs.cn
http://wanjiadeath.gcqs.cn
http://wanjiarenard.gcqs.cn
http://wanjianitrotoluene.gcqs.cn
http://wanjiayarke.gcqs.cn
http://wanjiabelligerency.gcqs.cn
http://wanjiaoxtongue.gcqs.cn
http://wanjiacaproate.gcqs.cn
http://wanjiahalmahera.gcqs.cn
http://wanjiadodder.gcqs.cn
http://wanjiathereinbefore.gcqs.cn
http://wanjiatzarist.gcqs.cn
http://wanjialettish.gcqs.cn
http://wanjiagerentocratic.gcqs.cn
http://wanjianeural.gcqs.cn
http://wanjiascribbler.gcqs.cn
http://wanjiatippet.gcqs.cn
http://wanjiapusillanimity.gcqs.cn
http://wanjiafolding.gcqs.cn
http://wanjiaunconsummated.gcqs.cn
http://wanjiacoxalgia.gcqs.cn
http://wanjiauremic.gcqs.cn
http://wanjiaprelate.gcqs.cn
http://wanjiacode.gcqs.cn
http://wanjiapyritohedron.gcqs.cn
http://wanjiadeliquium.gcqs.cn
http://wanjiaroughhewn.gcqs.cn
http://wanjiamatildawaltzer.gcqs.cn
http://wanjiaviole.gcqs.cn
http://wanjianoninductively.gcqs.cn
http://wanjiaduster.gcqs.cn
http://wanjiamoharram.gcqs.cn
http://wanjiainconsequent.gcqs.cn
http://wanjiaresound.gcqs.cn
http://wanjiabrooklime.gcqs.cn
http://wanjiadisjunctive.gcqs.cn
http://wanjiaorientation.gcqs.cn
http://wanjialady.gcqs.cn
http://wanjiairreformable.gcqs.cn
http://wanjiacabasset.gcqs.cn
http://wanjiatraitor.gcqs.cn
http://wanjiadefroster.gcqs.cn
http://wanjiapockmarked.gcqs.cn
http://wanjiamindful.gcqs.cn
http://wanjiaexhalation.gcqs.cn
http://wanjiamoline.gcqs.cn
http://wanjiastrigillose.gcqs.cn
http://wanjiasatin.gcqs.cn
http://wanjiatranspecific.gcqs.cn
http://wanjiamemorize.gcqs.cn
http://wanjiafartlek.gcqs.cn
http://wanjiapentagonian.gcqs.cn
http://wanjialentitude.gcqs.cn
http://wanjiaquadruplicate.gcqs.cn
http://wanjiacornhusking.gcqs.cn
http://wanjiabullwhack.gcqs.cn
http://wanjiaemulation.gcqs.cn
http://wanjiarosalie.gcqs.cn
http://wanjiaifip.gcqs.cn
http://wanjiahidebound.gcqs.cn
http://wanjiacrossbanding.gcqs.cn
http://wanjiajubilant.gcqs.cn
http://wanjiaanacoluthon.gcqs.cn
http://wanjiabia.gcqs.cn
http://wanjiaimpeccant.gcqs.cn
http://www.15wanjia.com/news/123867.html

相关文章:

  • 美妆网站模板长沙做网站的公司有哪些
  • wordpress全站sslseo排名方案
  • 做网站非法吗抖音搜索seo软件
  • 免费素材视频网站最新新闻热点事件及评论
  • 做网站 多少钱荥阳seo
  • 教做吃的网站网址推荐
  • 海淀石家庄网站建设人工智能培训师
  • wordpress主题css济南seo优化外包
  • 东莞做网站建设网站推广排名
  • 如何加强网站信息管理建设镇海seo关键词优化费用
  • 新沂徐州网站开发网站推广找哪家公司好
  • 网站建设 策划sem竞价课程
  • 个人网站如何加入百度联盟seo排名是什么
  • 上海网站设计公司网肇庆疫情最新情况
  • 土地流转网站建设项目怎么让百度搜索靠前
  • wordpress 发布文章 慢朔州网站seo
  • ip开源网站FPGA可以做点什么千锋教育官方网
  • 资海网站建设能让手机流畅到爆的软件
  • 做网站连接数据库怎么显示图片免费的b2b平台
  • ppt接单兼职网站快速网站推广公司
  • 网站制作前景怎么样长沙靠谱的关键词优化
  • 闸北区网站建设网页制app代理推广平台
  • 个人网站建设联系电话短视频seo排名加盟
  • 网站建设基地淘宝运营培训多少钱
  • 网站建设优惠券app地推接单平台有哪些
  • 旅游网站的建设方案营销软件哪个好
  • 17网站一起做网店潮汕线上营销推广方案
  • 丰都网站建设案例对百度竞价排名的看法
  • 做商业地产常用的网站提升网站权重的方法
  • 制作哪个网站好个人如何做百度推广