Dav*_*cía 3 events spring spring-mvc listener
Spring应用程序中有没有办法知道初始化何时完成?部署我的应用程序后,我必须运行一些代码,并且我正在搜索类似ServletContextListener或Spring内置事件的内容.
根据您对我的评论的回复,我将回答您可以做的多个事情来处理初始化的Spring bean.
BeanPostProcessor
.它有两个被视为回调的方法,我相信那postProcessAfterInitialization
是你会感兴趣的方法.问题BeanPostProcessor
是它们是为每个bean运行的ApplicationContext
,所以你要确保寻找只有您有兴趣应用此处理的bean.要使用a BeanPostProcessor
,您只需将其定义为您的一部分ApplicationContext
.InitializingBean
接口.它定义了一个afterPropertiesSet
由ApplicationContext
.调用的方法.这比1号更有优势,因为它可以在bean的基础上应用于bean(不适用于所有bean ApplicationContext
).@PostContstuct
在方法上使用注释.该注释告诉ApplicationContext
该方法应该在初始化bean之后运行.这类似于数字2,因为它是以bean为基础执行的.有关回调生命周期的更多信息,请ApplicationContext
参阅此位置.