One way to add custom ApplicationContextInitializer to spring web application is to add it in the web.xml file as shown below.
<
Another approach is to use context.initializer.classes=com.example.YourInitializer
in a properties/yml file. I like this approach because then you can enable/disable initializers via environment specific props files.
It is mentioned only briefly in the spring boot docs
You can register them in META-INF/spring.factories
org.springframework.context.ApplicationContextInitializer=\
com.example.YourInitializer
You can also add them on your SpringApplication
before running it
application.addInitializers(YourInitializer.class);
application.run(args);
Or on the builder
new SpringApplicationBuilder(YourApp.class)
.initializers(YourInitializer.class);
.run(args);
It wasn't obvious from the doc at first glance so I opened #5091 to check.