2

I am using Spring 4 (not Spring Boot) in the web application. I need to run some initialization code before any of the beans in the application context would be created. I tried to create implementation of org.springframework.context.ApplicationContextInitializer and register it in spring.factories but was not picked up for some reason. How can I do it?

briarheart
  • 1,906
  • 2
  • 19
  • 33

2 Answers2

3

As it turned out implementing of org.springframework.context.ApplicationContextInitializer was a right way. Because in my project I do not use Spring MVC implementation of this initializer should be registered in web.xml instead of spring.factories. Here is an example:

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>my.company.MyContextInitializer</param-value>
</context-param>
briarheart
  • 1,906
  • 2
  • 19
  • 33
0

This should work. If not, please post your code.

@Component
public class SampleBootstrap implements ApplicationListener<ContextRefreshedEvent> {

    ....

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Do Something();
    }
}
briarheart
  • 1,906
  • 2
  • 19
  • 33
Sc0rpion
  • 73
  • 1
  • 5
  • 1
    Thank you for the answer but as the name of event suggests this listener will be called after refresh of application context. What I need is quite opposite. Actually I've already found a solution. – briarheart Jun 27 '18 at 21:26