代码之家  ›  专栏  ›  技术社区  ›  mlo55

弹簧应用上下文加载挂钩

  •  6
  • mlo55  · 技术社区  · 16 年前

    我想在加载应用程序上下文之前(在实例化任何bean/properties/aspects/etc…之前)运行一段代码。

    提前谢谢

    3 回复  |  直到 15 年前
        1
  •  6
  •   Grzegorz Oledzki    16 年前

    大概 BeanFactoryPostProcessor s 能满足你的需要吗?它们在读取整个XML配置文件之后,但在实例化任何(其他)bean之前运行。

        2
  •  5
  •   Vladimir    16 年前

    您也可以使用 ApplicationListener 接收ContextClosedEvent、ContextStartedEvent或ContextStoppedEvent等事件的通知。

    更多信息请参阅 IoC Container

        3
  •  2
  •   Aritz    12 年前

    我刚刚宣布了我自己的 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>