一旦Spring应用程序成功部署,运行代码 [英] Run code once Spring app is succesfully deployed
本文介绍了一旦Spring应用程序成功部署,运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Spring应用程序中有没有办法知道初始化完成时间?我的应用程序部署后,我必须运行一些代码,我正在搜索一些类似ServletContextListener或 Spring built-在事件。任何想法?
Is there a way in a Spring app to know when the initialization has finished? I have to run some code once my app is deployed and I'm searching something like ServletContextListener or Spring built-in events. Any idea?
推荐答案
根据您对我的评论的回应,我将回应您可以处理初始化的Spring的多项内容bean。
Based on your response to my comment I will respond with the multiple things you can do to process an initialized Spring bean.
- 您可以使用
BeanPostProcessor
。它有两种方法被视为回调,我相信postProcessAfterInitialization
是你会感兴趣的。BeanPostProcessor
的东西是它们在ApplicationContext
中的每个bean运行,所以你一定要找到你有兴趣应用这个处理的bean。要使用BeanPostProcessor
,您只需将其定义为ApplicationContext
的一部分。 - 实施
InitializingBean
界面。它定义了一个单一的方法 ApplicationContext 调用的relPropertiesSet 之后的rel =noreferrer>。这比1号有一个优点,因为它可以以bean为基础应用(不适用于
ApplicationContext
中的所有bean)。 - 利用
@PostContuct
注释方法。该注释告诉ApplicationContext
,该方法应该在bean初始化之后运行。这样做与第2号类似,因为它是以豆为单位执行的。
- You can utilize a
BeanPostProcessor
. It has two methods that are treated as callbacks, and I believe thatpostProcessAfterInitialization
is the one that you would be interested in. The thing withBeanPostProcessor
's is that they are run for each bean in theApplicationContext
, so you will want to be sure to look for only the bean(s) that you are interested in applying this processing to. To use aBeanPostProcessor
, you simply define it as a part of yourApplicationContext
. - Implement the
InitializingBean
interface. It defines a single methodafterPropertiesSet
which is invoked by theApplicationContext
. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans inApplicationContext
). - Utilize the
@PostContstuct
annotation on a method. This annotation tells theApplicationContext
that this method should be run after the bean has been initialized. This acts similarly to number 2, in that it is performed on a bean by bean basis.
有关回调生命周期的更多信息 ApplicationContext
可以在此位置。
Further information on the callback lifecycle of the ApplicationContext
can be read about at this location.
这篇关于一旦Spring应用程序成功部署,运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文