Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quarkus application startup bean initalized twice

Tags:

quarkus

Following the guide https://quarkus.io/guides/lifecycle#startup_annotation, I created a class to initialize some metadata on startup with the following class:

@Startup
@ApplicationScoped
public class AppInstance {

    private final UUID id;
    private static final Logger logger = LoggerFactory.getLogger(AppInstance.class);

    AppInstance() {
        this.id = UUID.randomUUID();
        logger.info("App id: {}", this.id.toString());
    }

    public UUID getId() {
        return id;
    }
}

And when I run mvn quarkus:dev I see that the message App id: <some uuid> is logged twice.

Is this expected behaviour? If so what does this achieve?

like image 996
Rohit Sukumaran Avatar asked Oct 23 '25 18:10

Rohit Sukumaran


1 Answers

What version of quarkus do you use? It shouldn't be logged twice in quarkus 1.4.0+ unless the bean is injected somewhere.

The reason why the no-args constructor is called twice is that for normal scoped beans a client proxy is always created using a nor-ags constructor. And this client proxy is a subclass of the AppInstance.

In fact, you should not initialize a normal scoped bean like this but use a @PostConstruct callback instead.

You can also change the scope of your bean to javax.inject.Singleton in which case a client proxy is not used.

Anyway, this is a well-known limitation/feature of CDI normal scopes described in the spec and relevant resources.

like image 176
Martin Kouba Avatar answered Oct 26 '25 05:10

Martin Kouba