Spring ApplicationContext加载过程中是否存在任何钩子?Spring applicationcontext加载钩子
我想在应用程序上下文加载之前运行一段代码(在实例化任何bean/properties/aspects/etc之前)。
在此先感谢
Spring ApplicationContext加载过程中是否存在任何钩子?Spring applicationcontext加载钩子
我想在应用程序上下文加载之前运行一段代码(在实例化任何bean/properties/aspects/etc之前)。
在此先感谢
也许BeanFactoryPostProcessor
s会适合您的需求?它们在读取完整个XML配置文件之后运行,但在实例化任何(其他)bean之前。
您还可以使用ApplicationListener接收ContextClosedEvent,ContextStartedEvent或ContextStoppedEvent等事件的通知。
更多信息请登陆IoC Container一章。
我不认为'ApplicationListeners'得到通知*之前*环境启动,似乎没有这样的事件。来自ContextRefreshedEvent JavaDoc的 – skaffman 2009-11-16 13:52:06
:“ApplicationContext初始化或刷新时引发的事件。”如果事件在*初始化之前或之后发送,我今晚会重新开始。 – Vladimir 2009-11-16 16:11:19
我想在ContextStartedEvent上听,但似乎上下文的默认生命周期不明确地调用start方法,所以事件不会发布。在默认的生命周期中,我只是指整个Web服务器和上下文(我预计会触发ContextStartedEvent)的开始。任何想法,为什么? – Eugen 2011-05-09 08:24:11
我刚刚宣布我自己的ContextLoaderListener
为了在加载Spring上下文之前执行所需的工作。它适合于Web的应用程序,Spring上下文监听器之前刚刚宣布它:
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
//Perform your stuff here
}
}
<listener>
<listener-class>
com.myCompany.listeners.MyServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
很大,这就是我一直在后,非常感谢 – mlo55 2009-11-16 03:26:40