Skip to content

Commit ed0d8b4

Browse files
authored
Merge pull request quarkusio#36380 from mkouba/static-init-config-validation-docs
Config: docs for config value mismatch detection
2 parents 429a9ef + 591bd15 commit ed0d8b4

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

core/runtime/src/main/java/io/quarkus/runtime/annotations/StaticInitSafe.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.lang.annotation.Retention;
1010
import java.lang.annotation.Target;
1111

12-
import org.eclipse.microprofile.config.inject.ConfigProperty;
13-
1412
/**
1513
* Used to mark a configuration object as safe to be initialized during the STATIC INIT phase.
1614
* <p>

docs/src/main/asciidoc/config-reference.adoc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,68 @@ options exposed by a `org.eclipse.microprofile.config.Config` instance. Which me
706706
build configuration options since MicroProfile Config specification allows configuration sources not to expose all the property names they provide to users.
707707
====
708708

709+
== Config property values injected during static intialization phase
710+
711+
Quarkus collects the config property values injected in CDI beans during xref:writing-extensions.adoc#bootstrap-three-phases[static intialization phase].
712+
The collected values are then compared with their runtime initialization counterparts and if a mismatch is detected the application startup fails.
713+
How can it happen?
714+
For example, let's have a CDI bean `org.acme.MyBean`.
715+
`MyBean` injects a `@ConfigProperty` of name `foo` and is initialized during the native build.
716+
The config property does not exist during the native build and so the default value `bar` is used.
717+
But later, when the application is started the property is defined with a system property: `-Dfoo=baz`.
718+
This would lead to inconsistent state and unexpected behavior.
719+
Therefore, Quarkus would fail in this situation by default.
720+
721+
[source,java]
722+
----
723+
package org.acme;
724+
725+
import jakarta.enterprise.context.ApplicationScoped;
726+
import jakarta.enterprise.event.Observes;
727+
import jakarta.enterprise.context.Initialized;
728+
import org.eclipse.microprofile.config.inject.ConfigProperty;
729+
730+
@ApplicationScoped
731+
public class MyBean {
732+
733+
@ConfigProperty(name = "foo", defaultValue = "bar") <1>
734+
String foo;
735+
736+
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { <2>
737+
// this observer method is notified during STATIC_INIT...
738+
}
739+
}
740+
----
741+
<1> The config property is injected when the bean is created and the value is fixed.
742+
<2> In this particular case, the observer `@Initialized(ApplicationScoped.class)` caused the initialization of the bean. However, there are other possibilities. For example, some extensions initialize components during static intialization phase.
743+
744+
You can annotate an injected field/parameter with `@io.quarkus.runtime.annotations.StaticInitSafe` to mark the injected configuration object as safe to be initialized during the static intialization phase.
745+
746+
[source,java]
747+
----
748+
package org.acme;
749+
750+
import jakarta.enterprise.context.ApplicationScoped;
751+
import jakarta.enterprise.event.Observes;
752+
import jakarta.enterprise.context.Initialized;
753+
import org.eclipse.microprofile.config.inject.ConfigProperty;
754+
import io.quarkus.runtime.annotations.StaticInitSafe;
755+
756+
@ApplicationScoped
757+
public class MyBeanNoFailure {
758+
759+
@StaticInitSafe <1>
760+
@ConfigProperty(name = "foo", defaultValue = "bar")
761+
String foo;
762+
763+
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) {
764+
// this observer method is notified during STATIC_INIT...
765+
}
766+
}
767+
----
768+
<1> Instructs Quarkus not to fail if a mismatch is detected.
769+
770+
709771
[[additional-information]]
710772
== Additional Information
711773

0 commit comments

Comments
 (0)