I'm trying to find the correct way to execute some startup code which reads and uses tomcat's context.xml when running micronaut as a servlet.
I've tried creating a @Singleton
that implements ApplicationEventListener<ServerStartupEvent>
but that doesn't seem to get executed when my micronaut code is compiled and deployed as a war file servlet in tomcat.
Changing the @Singleton
to a @Context
annoation didn't help either.
The docs say that MicronautServletInitializer
will register a new DefaultMicronautServlet
but it doesn't look that DefaultMicronautServlet
is a bean that you can @Replace, it just hardcodes it in there.
Is there something special needed to get Application Events to trigger?
EDIT this also doesn't work:
package com.mypackage.api;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.context.event.StartupEvent;
import jakarta.inject.Singleton;
@Singleton
//@Context // Doesn't execute with or without
public class SystemUtilsConfig implements ApplicationEventListener<StartupEvent>
{
@Override
//@EventListener // doesn't execute with or without
//@Async // doesn't execute with or without
public void onApplicationEvent(StartupEvent startupEvent)
{
System.out.println("=====SystemUtilsConfig.onApplicationEvent======");
}
}
The ServerStartupEvent
is used with the embedded servers, which is not the case when you deploy the WAR to the existing Tomcat instance. If you want to run the code on startup in such a case, use ApplicationEventListener<io.micronaut.context.event.StartupEvent>
which will be executed after the application bootstrap is completed, no matter if the server is a running instance or an embedded one.