Naamabe
Naamabe

Reputation: 11

Quarkus @ConfigProperty does not inject value

I am using quarkus @ConfigProperty annotation in order to inject value to a String data member:

@ApplicationScoped
public class ActiveMQRout extends RouteBuilder {

    public String getOrderMessage() {
        return orderMessage;
    }
    public void setOrderMessage(String orderMessage) {
        this.orderMessage = orderMessage;
    }
    
    @ConfigProperty(name="order")
    String orderMessage;

//more code that uses orderMessage
}

In application.properties file I have this value: order=order2

the value of orderMessage stay null. I have tried using the following option instead of injection:

orderMessage = ConfigProvider.getConfig().getValue("order", String.class);

and it is working. how can I make this work with injection (by using @ConfigProperty annotation)?

(I have tried using the @Singleton annotation for the class ActiveMQRout but still did not work. I also added getter and setter but still did not work)

Upvotes: 1

Views: 339

Answers (1)

Eva M
Eva M

Reputation: 633

I had the same problem because I was using the config property in the constructor, but it's often not set yet when the constructor is called. I used @PostConstruct to use the config property and then it was not null anymore.

// THIS WORKS: Using the prop in a @PostConstruct method

@ApplicationScoped
public class BookRepository {

private final Set<Book> books = //...

@ConfigProperty(name = "books.genre")
String genre;

@PostConstruct
void init() {
    books.add(new Book(1, "The Great Gatsby", "F. Scott Fitzgerald", 1925, genre));
    books.add(new Book(2, "To Kill a Mockingbird", "Harper Lee", 1960, genre));
    books.add(new Book(3, "1984", "George Orwell", 1949, genre));
}

So be careful WHEN you call the property, because it might not contain its value yet.

Upvotes: 0

Related Questions