javadependency-injectionquarkuscdi

Quarkus Bean initialization order


I have a situation where one of my classes needs to perform some initialization tasks when the application starts up. This class depends on another component that also needs to be initialized at startup. I am using Quarkus and have annotated the dependent component with @Startup to ensure it gets initialized early. However, I am observing that the initialization method in my main class, which listens for the StartupEvent, is executing before the dependent component is initialized. This leads to issues such as the dependent component not being available when my main class tries to use it, resulting in exceptions. How can I ensure that the dependent component is fully initialized before my main class starts its initialization process in Quarkus?

In Spring I could use the @DependsOn which does not exist in Quarkus

I tried to use the @Priority but it seems to not work in my case.

Does anyone have an idea how I can control the initialisation sequence for those beans?


Solution

  • @Startup#value() can be used to specify the priority of the generated observer method and thus affects the ordering. In other words, if you annotate the class with @Startup(10) then it's translated to something like:

    void start(@Observes @Priority(10) StartupEvent event, YourBean bean) {
       // some code that makes sure the bean is initialized
    }