gpt4 book ai didi

java - 在 Micronaut 中有使用@PostConstruct 的正确方法吗?

转载 作者:行者123 更新时间:2023-11-29 08:23:19 28 4
gpt4 key购买 nike

我尝试在使用 @PostConstruct 启动应用程序后打印一条消息,但没有打印任何内容。

package dev.renansouza.server;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;

@Singleton
public class ServerService {

@PostConstruct
public void print() {
System.out.println("Hello!");
}
}

我读到@PostConstruct 是懒惰的。这是否意味着我需要做还有什么可以让它起作用的吗?

最佳答案

如果使用 @PostConstruct 对您来说不是那么重要,您也可以使用 @EventListener 注释来实现您想要的。

例如在您的情况下,您可以在任何类中添加以下代码来监听应用程序启动事件。

@EventListener
void onStartup(ServerStartupEvent event) {
println("Hey, I work from anywhere in project..")
}

Code shared above is in Groovy

请记住,根据我的观察,通常首先调用主应用程序类中添加的事件监听器。

关于java - 在 Micronaut 中有使用@PostConstruct 的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55506893/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com