英文:
Run code on micronaut application startup
问题
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.
我正在尝试找到在将Micronaut作为Servlet运行时执行启动代码以读取和使用Tomcat的context.xml的正确方法。
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.
我尝试创建一个@Singleton
,实现ApplicationEventListener<ServerStartupEvent>
,但当我将我的Micronaut代码编译并部署为Tomcat中的WAR文件Servlet时,似乎不会执行该代码。
Changing the @Singleton
to a @Context
annotation didn't help either.
将@Singleton
更改为@Context
注释也没有帮助。
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.
文档中表示MicronautServletInitializer
将注册一个新的DefaultMicronautServlet
,但似乎DefaultMicronautServlet
不是您可以用@Replace替换的bean,它只是硬编码在那里。
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;
importakarta.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=====");
}
}
英文:
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======");
}
}
答案1
得分: 5
ServerStartupEvent
用于嵌入式服务器,当你将 WAR 部署到现有的 Tomcat 实例时,情况就不同了。如果你想在这种情况下在启动时运行代码,请使用 ApplicationEventListener<io.micronaut.context.event.StartupEvent>
,它将在应用程序引导完成后执行,无论服务器是正在运行的实例还是嵌入式实例。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论