成功部署Spring应用程序后运行代码 [英] Run code once Spring app is successfully deployed
本文介绍了成功部署Spring应用程序后运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Spring应用程序中是否有办法知道初始化何时完成?部署应用程序后,我必须运行一些代码,并且正在搜索类似ServletContextListener或已构建Spring的代码-事件中.
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.
推荐答案
根据您对我的评论的响应,我将以多种方式响应您可以处理初始化的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
.它有两个被当作回调的方法,我相信InitializingBean
界面.它定义了一个单一方法afterPropertiesSet
,由ApplicationContext
调用.与数字1相比,它有一个优势,因为它可以逐个bean地应用(并非应用于ApplicationContext
中的所有bean). - 在
@PostContstuct
注释上方法.此注释告诉ApplicationContext
该方法应在bean初始化之后运行.这与数字2相似,因为它是在逐个bean的基础上执行的.
- 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屋!
查看全文