Search code examples
javaresteasyquarkusquarkus-rest-clienteclipse-microprofile-config

ConfigProperty not injecting the value into the field


I am new to quarkus environment. I have a quarkus application where I'm trying to inject the property config using

org.eclipse.microprofile.config.inject.ConfigProperty

Here is the sample code

public class Temp {

    @ConfigProperty(name = "secret.token")
    static String SECRET_KEY;

    public void display() {
        System.out.println(SECRET_KEY);
    }
}

Here is the content of my application.properties

secret.token = ${TOKEN_SECRET:Root}

Here display method is always printing null. The thing is the same property is being injected into the controller/resource endpoint classes properly but not in this class. I also tried using @Inject along with @ConfigProperty but no luck. Any pointers would really help.


Solution

  • The class on which the annotation is used, needs to be a CDI bean.

    The easiest way to accomplish that is to annotate the class with @Singleton and use with something like @Inject Temp temp wherever the class is used.

    See https://quarkus.io/guides/cdi for an intro to CDI