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
java spring


source to share


No one has answered this question yet

See similar questions:

13
Spring Boot: Read List from yaml using @Value or @ConfigurationProperties

or similar:

1818
How to get enum value from string value in Java?
708
How to configure port for Spring Boot application
519
Why is my Spring @Autowired field null?
341
Create the perfect JPA object
five
Testing a Spring Module Boot Service class with (out) repository in JUnit
4
Spring @ Custom constructor forces @Value to return null when instantiated in test class
3
Can't bind map to complex key from spring boot yaml using @ConfigurationProperties
1
Spring Boot - config value from yml is null
0
When I add @Autowire annotaion for the JAX WS class it gives an exclusive pointer exception error in spring boot
0
Spring Batch: @Value is null when other annotations are not



All Articles
Loading...
X
Show
Funny
Dev
Pics