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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With