From the Spring Boot documentation:
Some events are actually triggered before the ApplicationContext is
created, so you cannot register a listener on those as a @Bean. You
can register them with the SpringApplication.addListeners(…) method
or the SpringApplicationBuilder.listeners(…) method.
And from the Spring documentation (this for version 5.1.7):
Processing of @EventListener annotations is performed via the internal
EventListenerMethodProcessor bean which gets registered automatically
when using Java config or manually via the
or element when
using XML config.
So ApplicationEnvironmentPreparedEvent (and all of the Spring Boot application events) occur before context and bean creation, and @EventListener is controlled by a bean, so the @EventListener annotation cannot be picked up at this point. You will have to specifically create a listener class and add it explicitly to capture this event (or any event that occurs before bean creation).
Your class:
public class AppListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>
{
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event)
{
System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",
MyThreadFactory.class.getName());
}
}
...and the relevant SpringApplicationBuilder code:
ConfigurableApplicationContext context = new SpringApplicationBuilder(Launcher.class)
.listeners(new AppListener()).run();