@Value annotation doesn't work in Application.java

35 Views Asked by At

I am trying to get a property from my properties file (.yml) in my Application.java class using the @Value annotation. That property is a boolean, however, it is not able to find it, since it always returns false even if it is true, or returns null if I load it as a Boolean object.

However, if I perform the same operation from my Controller class, it does retrieve that property correctly (creates it to true).

What is this about? From what I've read, it seems that the beans are instantiated first, and then the Spring context (.yml). Is there a way to reverse this? I've tried implementing this interface ApplicationListener<ContextRefreshedEvent> , which is supposed to force the yml to be refreshed, but it keeps returning null.

That's my code:

@SpringBootApplication
@Slf4j
public class Application {

@Value("${backend.enabled}")
private boolean enabled;

public static void main(String[] args) {
    log.info(enabled); //Always false if boolean, always null if Boolean.
    SpringApplication.run(Application.class, args);
}

My .yml file:

backend:
  enabled: true 

How can I use the properties of the .yml in my Application.java?

0

There are 0 best solutions below