Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring applicationcontext loading hooks

Tags:

spring

Are there any hooks into the Spring ApplicationContext loading process?

I want to run a piece of code just before the application context loads (before any beans/properties/aspects/etc... are instantiated).

thanks in advance

like image 973
mlo55 Avatar asked Nov 15 '09 23:11

mlo55


3 Answers

Maybe BeanFactoryPostProcessors will suite your needs? They are run after the whole XML configuration files are read, but before any (other) beans are instantiated.

like image 172
Grzegorz Oledzki Avatar answered Sep 18 '22 01:09

Grzegorz Oledzki


You can also use the ApplicationListener to receive notification of events like ContextClosedEvent, ContextStartedEvent or ContextStoppedEvent.

More information in the IoC Container chapter.

like image 45
Vladimir Avatar answered Sep 22 '22 01:09

Vladimir


I just declared my own ContextLoaderListener in order to perform the desired work before loading the Spring context. It suits for web-apps, just declare it before the Spring context listener:

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>
like image 21
Xtreme Biker Avatar answered Sep 22 '22 01:09

Xtreme Biker