Getting null values ​​when using ConfigurationProperties on Spring Boot

I'm new to the Java world and Spring Boot and I'm trying to access some of the configuration values ​​located in a YAML file via annotation ConfigurationProperties

.

But whenever I try to access the config value anywhere in the service, I get a null value.

Here's the application.yml

file:

my_config:
  test: "plop"

      

Here's the config class ValidationProperties

:

@Configuration
@ConfigurationProperties(prefix = "my_config")
public class ValidationProperties {

    @NotNull
    private String test;

    public void setTest(String test) {
        this.test = test;
    }

    public String getTest() {
        return this.test;
    }
}

      

The authentication service that uses it:

@Service
public class MyValidator implements ConstraintValidator<MyConstraint, MyEntity> {

    @Autowired
    private ValidationProperties validationProperties;

    @Value("${my_config.test}")
    private String test;

    @Override
    public boolean isValid(MyEntity entity, ConstraintValidatorContext context) {

        System.out.println(this.test); // null value, why?
    }
}

      

I also added annotation @EnableConfigurationProperties

in my main class.

I'm not sure what annotation is supposed to do, but I am obviously missing something here. Also, if I try to access the value from the getter of the config file, I get the exception:

System.out.println(this.validationProperties.getTest());

      

will get me HV000028: Unexpected exception during isValid call.

+3


source to share





All Articles