Is there a proper way to use @PostConstruct in Micronaut?

I’m trying to print a message after the application startup with @PostConstruct, but nothing is printed.

package dev.renansouza.server;

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

@Singleton
public class ServerService {

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

I have read that @PostConstruct is Lazy. Does this mean that I need to do something else for this to work?

Answer

You can also use @EventListener annotation to acheive what you what, if using @PostConstruct is not that important to you.

For example in your case, you can add following code in any class to listen for application startup event.

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

Code shared above is in Groovy

Keep in mind, the event listener added in main application class is usually called first from what I have observed.