说明: 本文基于Spring-Framework 5.1.x版本讲解
说起生命周期, 很多开源框架、中间件的组件都有这个词,其实就是指组件从创建到销毁的过程。 那这里讲Spring Bean的生命周期,并不是讲Bean是如何创建的, 而是想讲下Bean从实例化到销毁,Spring框架在Bean的各个阶段给我们提供了哪些拓展点。 Bean本身有三个大的阶段: 实例化、初始化、销毁。
Spring的强大就是提供了非常多的拓展点, 我们可以基于这些拓展点实现不同的需求。 回到主题,Spring容器围绕着生命周期的各个阶段提供了不同功能的拓展点如下图:

从上图可以看到,整个生命周期涉及到的接口(当然这不是所有接口,只是日常中可以用到的,还有一部分是Spring内部的接口)分成了4个阶段:
1. 实例化阶段: 主要是以不同方式实例化Bean
2. 属性注入阶段:IOC的过程
3. 初始化阶段:初始化Bean的内部组件、生成代理对象等都在这里
4. 销毁阶段: 释放资源
下面我们对这些核心的接口进行简单的介绍
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
返回值Object: 返回null值则执行Spring提供给我们的Bean实例化、属性注入、初始化阶段, 这是对于大多数Bean的选择; 返回非null值则跳过Bean实例化、属性注入、初始化阶段,因为Spring会认为IOC等阶段由使用者管理,所以在这种情况下后续会直接调用BeanPostProcessor#postProcessAfterInitialization接口, 进而有可能提前结束标准IOC流程, 为什么说有可能呢? 因为还需取决于BeanPostProcessor#postProcessAfterInitialization的返回值。
使用场景: 使用频率比较低,实际没有用到过。不过在Spring-Framework的AOP源码中有用到,见AbstractAutoProxyCreator
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
返回值boolean: true则执行Spring的属性注入功能 , 大多数情况返回true即可,除非你有定制化的需求;返回false跳过Spring属性注入功能,意味着@Resource、@Autowired等注解失效,也意味着xml文件中<property>标签失效。
使用场景: 比较少, Spring-Framework源码中也没有用到
@Nullable
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}
返回值PropertyValues: 一般返回入参pvs即可, Spring后续会使用返回值进行属性注入。 尤其对于SpringBoot这种纯JavaConfig配置的方式, 参数pvs一般为Empty(注意与null区分)
使用场景:标注@Resource、@Autowired等注解的属性都在这个方法实现注入,比较核心, 详见: CommonAnnotationBeanPostProcessor、AutowiredAnnotationBeanPostProcessor
如果postProcessProperties方法返回null,Spring也会调用postProcessPropertyValues方法实现同样的效果,不过这个方法是老版本中的,已经弃用掉的,不推荐使用.
@Deprecated
@Nullable
default PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
return pvs;
}
public interface BeanNameAware extends Aware {
void setBeanName(String name);
}
public interface BeanClassLoaderAware extends Aware {
void setBeanClassLoader(ClassLoader classLoader);
}
public interface BeanFactoryAware extends Aware {
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
这三个接口就比较简单了,就是给我们的Bean注入BeanName、ClassLoader、BeanFactory属性, 他的调用时机在实例化、属性注入之后,是初始化阶段的第一步
/**
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
返回值Object: 一般情况返回入参的bean即可。
需要注意几点:
1. 如果返回null,则后续所有实现该接口的Processor都不会执行,且返回上个Processor的返回值;
2. 上个Processor的返回值会作为下个Processor的入参。
3. 最后一个Processor的返回值会代替原来的Bean(返回入参Bean的情况可以忽略这一条)进行后续处理(包含Bean初始化、以及最终暴露到Spring容器中)
这里比较难理解,贴下源码
/**
* 执行Bean的初始化步骤
*/
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
// 此处省略不关键的部分代码
Object wrappedBean = bean;
// 调用所有实现BeanPostProcessor#postProcessBeforeInitialization接口的方法
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
// 使用postProcessBeforeInitialization方法的返回值当做入参进行Bean初始化
invokeInitMethods(beanName, wrappedBean, mbd);
// 使用postProcessBeforeInitialization方法的返回值当做入参进行Bean初始化的后置处理
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
return wrappedBean;
}
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
// getBeanPostProcessors() 返回的Processors是有有优先级顺序的
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result; // 如果为null , 这里直接退出该方法并返回上次Processor返回的结果
}
result = current; // 上个Processor执行的结果作为下个Processor处理的入参
}
return result;
}
使用场景:1. 标注@PostConstruct注解的方法都在该接口实现调用, 详见InitDestroyAnnotationBeanPostProcessor 2. 各种Aware接口的调用入口,详见ApplicationContextAwareProcessor
public interface InitializingBean {
/**
* Invoked by the containing {@code BeanFactory} after it has set all bean properties
* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
* <p>This method allows the bean instance to perform validation of its overall
* configuration and final initialization when all bean properties have been set.
* @throws Exception in the event of misconfiguration (such as failure to set an
* essential property) or if initialization fails for any other reason
*/
void afterPropertiesSet() throws Exception;
}
Bean初始化资源的回调接口 , 与@PostConstruct一样的作用 ,但要注意的是几个初始化方法调用的顺序:1.@PostConstruct2. afterPropertiesSet 3. init-method
使用场景: 资源初始化、可以使用回调的特性实现策略模式等,实际工作中用的较多
XML配置文件中init-method属性或者@Bean注解initMethod属性指定的方法 , 比较简单不多说了
/**
* Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other BeanPostProcessor callbacks.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
返回值Object: 一般情况返回入参的bean即可。
与postProcessBeforeInitialization方法的调用逻辑一样,需要注意几点:
1. 如果返回null,则后续所有实现该接口的Processor都不会执行,且返回上个Processor的返回值;
2. 上个Processor的返回值会作为下个Processor的入参。
3. 最后一个Processor的返回值会代替原来的Bean(返回入参Bean的情况可以忽略这一条)进行后续处理(包含Bean初始化、以及最终暴露到Spring容器中)
使用场景:AOP生成代理的入口,详见AbstractAutoProxyCreator
/**
* Apply this BeanPostProcessor to the given bean instance before its
* destruction, e.g. invoking custom destruction callbacks.
* <p>Like DisposableBean's {@code destroy} and a custom destroy method, this
* callback will only apply to beans which the container fully manages the
* lifecycle for. This is usually the case for singletons and scoped beans.
* @param bean the bean instance to be destroyed
* @param beanName the name of the bean
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.DisposableBean#destroy()
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#setDestroyMethodName(String)
*/
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
用于处理Bean销毁前的一些前置工作,可实现批量处理。
使用场景:标注@PreDestroy注解的方法都在该接口实现调用, 详见InitDestroyAnnotationBeanPostProcessor
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
* @throws Exception in case of shutdown errors. Exceptions will get logged
* but not rethrown to allow other beans to release their resources as well.
*/
void destroy() throws Exception;
Bean直接实现该接口即可,同时也注意这几个销毁方法的调用顺序: 1.@PreDestroy2. destroy 3. destroy-method
使用场景:释放Bean本身持有的资源,如连接池Bean资源等 ,单个处理。
XML配置文件中destroy-method属性或者@Bean注解destroyMethod属性指定的方法 , 比较简单不多说了
围绕生命周期Spring容器给我们提供的接口就简单介绍到这,实际上除了上面列举的接口之外,还有一些不常用的接口我没有列举出来,如:SmartInstantiationAwareBeanPostProcessor、MergedBeanDefinitionPostProcessor等。对于以上接口,个人理解没有必要像八股文一样死记硬背每个接口的作用,甚至尝试记住接口的名称,关键在于设计的思想,开头也说过,其他框架的组件也有生命周期的实现,Spring与这些框架实现上有什么不同,这才是应该了解的;当然完全不了解其接口的作用也是不行的,这里强调的是不用刻意去死记硬背接口的含义, 在工作中多看看别人、别的框架的使用方式更有利于加深理解。
我在Ruby中有很多时间范围:period=Time.parse('8:00am')..Time.parse('8:00pm')incidents=[Time.parse('7:00am')..Time.parse('9:00am'),Time.parse('1:00pm')..Time.parse('3:00pm'),Time.parse('1:30pm')..Time.parse('3:30pm'),Time.parse('7:00pm')..Time.parse('9:00pm'),]我正试图在这段时间内获得一系列无事件block。对于以上内容:[Time.parse('9:00
unicorn有OobGC可用于在一定数量的请求后运行GC.start的机架中间件。PhusionPassenger中有类似的东西吗? 最佳答案 PhusionPassenger4正式引入了带外垃圾回收机制。它比Unicorn更灵活,允许任意工作,而不仅仅是垃圾收集。http://blog.phusion.nl/2013/01/22/phusion-passenger-4-technology-preview-out-of-band-work/ 关于ruby-on-rails-有没有一种
我有一个rubyonrails应用程序,我试图在其中找到每隔几秒运行一些代码的方法。我发现了很多使用cron或类似cron的实现的信息和想法,但这些只是准确到分钟,并且/或需要外部工具。我想每15秒左右启动一次任务,并且我希望它完全独立于应用程序中(如果应用程序停止,任务也停止,并且没有外部设置)。这用于缓存数据的后台生成。每隔几秒,任务就会收集一些数据,然后将其存储在缓存中,供所有客户端请求使用。该任务非常慢,因此需要在后台运行并且不阻塞客户端请求。我是ruby的新手,但有很强的perl背景,我解决这个问题的方法是创建一个间隔计时器和处理程序,它fork、运行代码,然后在完成
我正在尝试了解Rails应用程序的生命周期。application_controller.rb什么时候运行?是每次更改时只执行一次,还是每次请求时都执行一次?我想了解以下文件:config/environments/*.rb(开发、生产或测试,取决于当前模式)boot.rb环境.rb路线.rb我问这个的原因之一是,我想知道放在哪里比较好初始化代码自定义配置数据编辑:@Gdeglin的回答很好,但我实际上很想知道这些文件中的每一个何时运行。 最佳答案 应用程序Controller.rbApplicationController是所有C
在Sinatra中,我无法创建在应用程序生命周期中仅分配一次值的全局变量。我错过了什么吗?我的简化代码如下所示:require'rubygems'ifRUBY_VERSION这导致nil2在终端和,2在浏览器中。如果我尝试将@a=1放入initialize方法中,我会在WebApp.run!中遇到错误线。我觉得我错过了一些东西,因为如果我不能有全局变量,那么我如何在应用程序实例化期间加载大数据?beforedo似乎每次有来自客户端的请求时都会被调用。 最佳答案 classWebApp请注意,如果您使用Shotgun或其他在每次请求时
所以我有一个使用React和Ajax调用的有趣案例。在上下文中,我有一个带有3个选项卡的Accordion。初始化Accordionreact组件后,我首先打开第一个选项卡,其余选项卡关闭。每个选项卡的主体中都有所谓的DictionaryCall组件,如下所示:returnclassDictionaryCallextendsReact.Component{constructor(props){super();this.state={word:'',data:[],error:false,nodata:false,initialLoaded:props.load}}componentDi
我正在Javascript上构建自定义slider,我希望每次用户单击slider的div时,slider都应停止X秒。我的代码是:$(document).ready(function(){varciclo;varindex_slide=1;functionstartSlidercicle(){ciclo=setInterval(function(){//Slidercodegoeshere},3000);}//HereIstarttheslideranimationstartSlidercicle();//Whentheuserclicksonadivcalled'slide',st
这是一个例子:混入.jsexportdefault{methods:{aFunction(){//Somefunctionalityhere}}}组件.vueimportmixinfrom'./mixin'exportdefault{mixins:[mixin]created(){//CallaFunctiondefinedinthemixinhere}}我想从组件内部的created()生命周期方法访问在mixin方法内部定义的aFunction。 最佳答案 mixin方法与组件的当前实例合并,所以它只是:created(){th
我想知道javascriptblock/函数是否在加载后始终可用。因为我已经测试了一些东西,现在我有点困惑。我将一个脚本block定义到一个div中。脚本block有一个事件处理函数,用于元素使用ajax重新加载div。ajax调用返回div的纯html并将其替换为当前的html。但这意味着替换执行的脚本。我认为脚本会在替换语句后停止执行。但它没有。执行替换语句后的代码行那么这些东西是如何工作的。您如何描述脚本block的生命周期? 最佳答案 当代码包含在script中时元素被求值时,代码求值的结果成为页面运行时环境的一部分。删除s
我正在实现处理选项卡的GoogleChrome扩展程序。这包括我获取onCreated、onUpdated和onActivated。每次,我都在我的逻辑中使用tabId,它最初在处理onCreated事件期间存储在一个数组中。原则上,一切正常。但是,我注意到了一个小故障。有时,现有选项卡的ID会更改。因此,一个选项卡有一个不在我的数组中的ID,这自然会导致错误。我可以在以下用例中重现此问题:使用不同的URL打开2个或更多标签在一个选项卡中加载一个已在另一个选项卡中加载的URL在这种情况下,会发生两件事:首先,触发T的onActivated事件,而不是onUpdated事件。其次,T现在