成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring奇技淫巧之?dāng)U展點(diǎn)的應(yīng)用

開發(fā) 架構(gòu)
bean生命周期的最后一個(gè)擴(kuò)展點(diǎn),該方法用于執(zhí)行一些bean銷毀前的準(zhǔn)備工作,比如將當(dāng)前bean持有的一些資源釋放掉。

[[392369]]

本文轉(zhuǎn)載自微信公眾號(hào)「月伴飛魚」,作者日常加油站。轉(zhuǎn)載本文請(qǐng)聯(lián)系月伴飛魚公眾號(hào)。  

最近在看公司項(xiàng)目和中間件的時(shí)候,看到一些Spring擴(kuò)展點(diǎn)的使用,寫篇文章學(xué)習(xí)下,對(duì)大家之后看源碼都有幫助

「首先先介紹下Bean的生命周期」

我們知道Bean的生命周期分為幾個(gè)主干流程

  • Bean(單例非懶加載)的實(shí)例化階段
  • Bean的屬性注入階段
  • Bean的初始化階段
  • Bean的銷毀階段

下面是整個(gè)Spring容器的啟動(dòng)流程,可以看到除了上述幾個(gè)主干流程外,Spring還提供了很多擴(kuò)展點(diǎn)

下面詳細(xì)介紹下Spring的常見的擴(kuò)展點(diǎn)

Spring常見擴(kuò)展點(diǎn)

「BeanFactoryPostProcessor#postProcessBeanFactory」

有時(shí)候整個(gè)項(xiàng)目工程中bean的數(shù)量有上百個(gè),而大部分單測依賴都是整個(gè)工程的xml,導(dǎo)致單測執(zhí)行時(shí)需要很長時(shí)間(大部分時(shí)間耗費(fèi)在xml中數(shù)百個(gè)單例非懶加載的bean的實(shí)例化及初始化過程)

解決方法:利用Spring提供的擴(kuò)展點(diǎn)將xml中的bean設(shè)置為懶加載模式,省去了Bean的實(shí)例化與初始化時(shí)間

  1. public class LazyBeanFactoryProcessor implements BeanFactoryPostProcessor { 
  2.     @Override 
  3.     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 
  4.         DefaultListableBeanFactory fac = (DefaultListableBeanFactory) beanFactory; 
  5.         Map<String, AbstractBeanDefinition> map = (Map<String, AbstractBeanDefinition>) ReflectionTestUtils.getField(fac, "beanDefinitionMap"); 
  6.         for (Map.Entry<String, AbstractBeanDefinition> entry : map.entrySet()) { 
  7.             //設(shè)置為懶加載 
  8.             entry.getValue().setLazyInit(true); 
  9.         } 
  10.     } 

「InstantiationAwareBeanPostProcessor#postProcessPropertyValues」

非常規(guī)的配置項(xiàng)比如

  1. <context:component-scan base-package="com.zhou" /> 

Spring提供了與之對(duì)應(yīng)的特殊解析器

正是通過這些特殊的解析器才使得對(duì)應(yīng)的配置項(xiàng)能夠生效

而針對(duì)這個(gè)特殊配置的解析器為 ComponentScanBeanDefinitionParser

在這個(gè)解析器的解析方法中,注冊(cè)了很多特殊的Bean

  1. public BeanDefinition parse(Element element, ParserContext parserContext) { 
  2.   //... 
  3.   registerComponents(parserContext.getReaderContext(), beanDefinitions, element); 
  4.     //... 
  5.   return null
  1. public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors( 
  2.    BeanDefinitionRegistry registry, Object source) { 
  3.  
  4.   Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<BeanDefinitionHolder>(4); 
  5.   //... 
  6.     //@Autowire 
  7.   if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) { 
  8.    RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class); 
  9.    def.setSource(source); 
  10.    beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); 
  11.   } 
  12.  
  13.   // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor. 
  14.    //@Resource 
  15.   if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) { 
  16.       //特殊的Bean 
  17.    RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class); 
  18.    def.setSource(source); 
  19.    beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); 
  20.   } 
  21.   //... 
  22.   return beanDefs; 
  23.  } 

以@Resource為例,看看這個(gè)特殊的bean做了什么

  1. public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor 
  2.   implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable { 
  3.       
  4.       public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds,  
  5.       Object bean, String beanName) throws BeansException { 
  6.           InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass()); 
  7.           try { 
  8.             //屬性注入 
  9.             metadata.inject(bean, beanName, pvs); 
  10.           } 
  11.           catch (Throwable ex) { 
  12.             throw new BeanCreationException(beanName, "Injection of resource dependencies failed", ex); 
  13.           } 
  14.           return pvs; 
  15.     } 
  16.      

我們看到在postProcessPropertyValues方法中,進(jìn)行了屬性注入

「invokeAware」

實(shí)現(xiàn)BeanFactoryAware接口的類,會(huì)由容器執(zhí)行setBeanFactory方法將當(dāng)前的容器BeanFactory注入到類中

  1. @Bean 
  2. class BeanFactoryHolder implements BeanFactoryAware{ 
  3.     
  4.     private static BeanFactory beanFactory; 
  5.      
  6.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 
  7.         this.beanFactory = beanFactory; 
  8.     } 

「BeanPostProcessor#postProcessBeforeInitialization」

實(shí)現(xiàn)ApplicationContextAware接口的類,會(huì)由容器執(zhí)行setApplicationContext方法將當(dāng)前的容器applicationContext注入到類中

  1. @Bean 
  2. class ApplicationContextAwareProcessor implements BeanPostProcessor { 
  3.  
  4.     private final ConfigurableApplicationContext applicationContext; 
  5.  
  6.     public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) { 
  7.       this.applicationContext = applicationContext; 
  8.     } 
  9.  
  10.     @Override 
  11.     public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { 
  12.       //... 
  13.       invokeAwareInterfaces(bean); 
  14.       return bean; 
  15.     } 
  16.  
  17.     private void invokeAwareInterfaces(Object bean) { 
  18.         if (bean instanceof ApplicationContextAware) { 
  19.           ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); 
  20.         } 
  21.     } 

我們看到是在BeanPostProcessor的postProcessBeforeInitialization中進(jìn)行了setApplicationContext方法的調(diào)用

  1. class ApplicationContextHolder implements ApplicationContextAware{ 
  2.     
  3.     private static ApplicationContext applicationContext; 
  4.      
  5.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
  6.         this.applicationContext = applicationContext; 
  7.     } 

「afterPropertySet()和init-method」

目前很多Java中間件都是基本Spring Framework搭建的,而這些中間件經(jīng)常把入口放到afterPropertySet或者自定義的init中

「BeanPostProcessor#postProcessAfterInitialization」

熟悉aop的同學(xué)應(yīng)該知道,aop底層是通過動(dòng)態(tài)代理實(shí)現(xiàn)的

當(dāng)配置了時(shí)候,默認(rèn)開啟aop功能,相應(yīng)地調(diào)用方需要被aop織入的對(duì)象也需要替換為動(dòng)態(tài)代理對(duì)象

不知道大家有沒有思考過動(dòng)態(tài)代理是如何「在調(diào)用方無感知情況下替換原始對(duì)象」的?

根據(jù)上文的講解,我們知道:

  1. <aop:aspectj-autoproxy/> 

Spring也提供了特殊的解析器,和其他的解析器類似,在核心的parse方法中注冊(cè)了特殊的bean

這里是一個(gè)BeanPostProcessor類型的bean

  1. class AspectJAutoProxyBeanDefinitionParser implements BeanDefinitionParser { 
  2.  @Override 
  3.  public BeanDefinition parse(Element element, ParserContext parserContext) { 
  4.     //注冊(cè)特殊的bean 
  5.   AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element); 
  6.   extendBeanDefinition(element, parserContext); 
  7.   return null
  8.     } 

將于當(dāng)前bean對(duì)應(yīng)的動(dòng)態(tài)代理對(duì)象返回即可,該過程對(duì)調(diào)用方全部透明

  1. public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator { 
  2.   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 
  3.         if (bean != null) { 
  4.           Object cacheKey = getCacheKey(bean.getClass(), beanName); 
  5.           if (!this.earlyProxyReferences.containsKey(cacheKey)) { 
  6.             //如果該類需要被代理,返回動(dòng)態(tài)代理對(duì)象;反之,返回原對(duì)象 
  7.             return wrapIfNecessary(bean, beanName, cacheKey); 
  8.           } 
  9.         } 
  10.         return bean; 
  11.  } 

正是利用Spring的這個(gè)擴(kuò)展點(diǎn)實(shí)現(xiàn)了動(dòng)態(tài)代理對(duì)象的替換

「destroy()和destroy-method」

bean生命周期的最后一個(gè)擴(kuò)展點(diǎn),該方法用于執(zhí)行一些bean銷毀前的準(zhǔn)備工作,比如將當(dāng)前bean持有的一些資源釋放掉

 

責(zé)任編輯:武曉燕 來源: 月伴飛魚
相關(guān)推薦

2020-05-20 19:38:11

前端js調(diào)試工具

2022-01-07 14:50:46

VS CodeLinux代碼

2017-08-18 13:30:01

前端CSS布局奇技

2023-06-26 08:05:36

2020-11-26 11:45:31

Python繪圖代碼

2017-10-24 13:42:55

流氓App安卓Google

2021-03-30 07:47:46

SVG 濾鏡 CSS技巧

2022-09-30 12:55:14

Linux筆記

2022-04-21 15:00:53

LinuxShell

2023-09-19 08:03:50

rebase?merge

2015-04-13 13:21:45

JavaScript JavaScript

2021-02-25 09:19:11

LinuxAppimage命令

2021-05-18 13:05:31

LinuxRust復(fù)用器

2021-06-07 12:20:14

LinuxASCII命令

2019-04-25 13:10:04

Java 8Stream API編程語言

2022-04-24 16:00:15

LinuxLinux命令ls命令

2024-11-28 09:21:00

Python字符串代碼

2021-05-07 13:56:13

Linux器監(jiān)視服務(wù)器

2021-05-31 11:45:37

LinuxRustShell

2023-09-28 08:49:41

springBean
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久午夜视频 | 国产婷婷精品av在线 | 久久精品免费一区二区 | 在线欧美一区 | 一区二区中文 | 精品91av| 亚洲视频 欧美视频 | 99久久国产综合精品麻豆 | 精精精精xxxx免费视频 | 欧美老少妇一级特黄一片 | 中文字幕亚洲区一区二 | 九九九视频 | 亚洲成色777777在线观看影院 | 午夜精品在线 | 成人av免费在线观看 | 91免费看片 | 精品在线一区二区三区 | 欧美性久久 | 国产精品99久久久久久www | 九九在线视频 | 特级特黄特色的免费大片 | 精品在线一区 | 久久中文字幕av | 日本不卡免费新一二三区 | jav成人av免费播放 | 欧美情趣视频 | 国产精品免费福利 | 激情视频中文字幕 | 91社区视频| 中文字幕视频三区 | 日韩一区二区免费视频 | 国产欧美在线播放 | 日本a视频 | 国产1区| 伊人91在线 | 国内精品一区二区三区 | 日韩福利 | 欧美色综合网 | 青青青伊人 | 日本福利在线 | 日日操操 |