Search code examples
javacdiquarkusinject

Java Quarkus how can I pass @ConfigProperty to an constructor?


I have the following class:

@ApplicationScoped
public class AppScopeTest {

    @ConfigProperty(name = "my-config-prop")
    String test;
    
    private TestClass testclass;

    @Inject
    public AppScopeTest () {
        this.testClass= new TestClass (test);
    }
}

I have a few classes that want a new instance of TestClass created once that class is created. example is shown above. But if I do it this way the string Test is always null inside the class TestClass.

So my questions is how can I read a config property create a new instance of TestClass that takes that config property as an argument?

I don't want this to be a method call because when AppScopeTest is created I want an instance of TestClass


Solution

  • How about doing it this way:

    @ApplicationScoped
    public class AppScopeTest {
    
        private final String test;
        
        private TestClass testclass;
    
        @Inject
        public AppScopeTest (@ConfigProperty(name = "my-config-prop") String test) {
            this.testClass = new TestClass(test);
        }
    }