Spring奇技淫巧之扩展点的应用

开发 架构
bean生命周期的最后一个扩展点,该方法用于执行一些bean销毁前的准备工作,比如将当前bean持有的一些资源释放掉。

[[392369]]

本文转载自微信公众号「月伴飞鱼」,作者日常加油站。转载本文请联系月伴飞鱼公众号。  

最近在看公司项目和中间件的时候,看到一些Spring扩展点的使用,写篇文章学习下,对大家之后看源码都有帮助

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

我们知道Bean的生命周期分为几个主干流程

  • Bean(单例非懒加载)的实例化阶段
  • Bean的属性注入阶段
  • Bean的初始化阶段
  • Bean的销毁阶段

下面是整个Spring容器的启动流程,可以看到除了上述几个主干流程外,Spring还提供了很多扩展点

下面详细介绍下Spring的常见的扩展点

Spring常见扩展点

「BeanFactoryPostProcessor#postProcessBeanFactory」

有时候整个项目工程中bean的数量有上百个,而大部分单测依赖都是整个工程的xml,导致单测执行时需要很长时间(大部分时间耗费在xml中数百个单例非懒加载的bean的实例化及初始化过程)

解决方法:利用Spring提供的扩展点将xml中的bean设置为懒加载模式,省去了Bean的实例化与初始化时间

  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.             //设置为懒加载 
  8.             entry.getValue().setLazyInit(true); 
  9.         } 
  10.     } 

「InstantiationAwareBeanPostProcessor#postProcessPropertyValues」

非常规的配置项比如

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

Spring提供了与之对应的特殊解析器

正是通过这些特殊的解析器才使得对应的配置项能够生效

而针对这个特殊配置的解析器为 ComponentScanBeanDefinitionParser

在这个解析器的解析方法中,注册了很多特殊的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为例,看看这个特殊的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方法中,进行了属性注入

「invokeAware」

实现BeanFactoryAware接口的类,会由容器执行setBeanFactory方法将当前的容器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」

实现ApplicationContextAware接口的类,会由容器执行setApplicationContext方法将当前的容器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中进行了setApplicationContext方法的调用

  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搭建的,而这些中间件经常把入口放到afterPropertySet或者自定义的init中

「BeanPostProcessor#postProcessAfterInitialization」

熟悉aop的同学应该知道,aop底层是通过动态代理实现的

当配置了时候,默认开启aop功能,相应地调用方需要被aop织入的对象也需要替换为动态代理对象

不知道大家有没有思考过动态代理是如何「在调用方无感知情况下替换原始对象」的?

根据上文的讲解,我们知道:

  1. <aop:aspectj-autoproxy/> 

Spring也提供了特殊的解析器,和其他的解析器类似,在核心的parse方法中注册了特殊的bean

这里是一个BeanPostProcessor类型的bean

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

将于当前bean对应的动态代理对象返回即可,该过程对调用方全部透明

  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.             //如果该类需要被代理,返回动态代理对象;反之,返回原对象 
  7.             return wrapIfNecessary(bean, beanName, cacheKey); 
  8.           } 
  9.         } 
  10.         return bean; 
  11.  } 

正是利用Spring的这个扩展点实现了动态代理对象的替换

「destroy()和destroy-method」

bean生命周期的最后一个扩展点,该方法用于执行一些bean销毁前的准备工作,比如将当前bean持有的一些资源释放掉

 

责任编辑:武晓燕 来源: 月伴飞鱼
相关推荐

2020-05-20 19:38:11

前端js调试工具

2017-08-18 13:30:01

前端CSS布局奇技

2022-01-07 14:50:46

VS CodeLinux代码

2023-06-26 08:05:36

2017-10-24 13:42:55

流氓App安卓Google

2021-03-30 07:47:46

SVG 滤镜 CSS技巧

2020-11-26 11:45:31

Python绘图代码

2022-04-21 15:00:53

LinuxShell

2022-09-30 12:55:14

Linux笔记

2023-09-19 08:03:50

rebase​merge

2021-06-07 12:20:14

LinuxASCII命令

2015-04-13 13:21:45

JavaScript JavaScript

2021-02-25 09:19:11

LinuxAppimage命令

2021-05-18 13:05:31

LinuxRust复用器

2019-04-25 13:10:04

Java 8Stream API编程语言

2022-04-24 16:00:15

LinuxLinux命令ls命令

2021-05-31 11:45:37

LinuxRustShell

2021-05-07 13:56:13

Linux器监视服务器

2023-09-28 08:49:41

springBean

2020-11-27 10:33:11

Linuxcpmv命令
点赞
收藏

51CTO技术栈公众号