成功部署Spring应用程序后运行代码 [英] Run code once Spring app is successfully deployed

查看:74
本文介绍了成功部署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.

  1. 您可以使用 BeanPostProcessor .它有两个被当作回调的方法,我相信 InitializingBean 界面.它定义了一个单一方法 afterPropertiesSet ,由ApplicationContext调用.与数字1相比,它有一个优势,因为它可以逐个bean地应用(并非应用于ApplicationContext中的所有bean).
  2. @PostContstuct 注释上方法.此注释告诉ApplicationContext该方法应在bean初始化之后运行.这与数字2相似,因为它是在逐个bean的基础上执行的.
  1. You can utilize a BeanPostProcessor. It has two methods that are treated as callbacks, and I believe that postProcessAfterInitialization is the one that you would be interested in. The thing with BeanPostProcessor's is that they are run for each bean in the ApplicationContext, 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 a BeanPostProcessor, you simply define it as a part of your ApplicationContext.
  2. Implement the InitializingBean interface. It defines a single method afterPropertiesSet which is invoked by the ApplicationContext. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans in ApplicationContext).
  3. Utilize the @PostContstuct annotation on a method. This annotation tells the ApplicationContext 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆