My Source code:
Person.java
@Component
@Validated
@PropertySource(value = "classpath:person.yaml")
@Data
public class Person {
@NotNull(message = "test can't be null")
private String test;
@Value("${name}")
private String name;
@Autowired
private Cat pet;
@Max(value=120,message="invalid age")
@Value("${age}")
private int age;
@Email(message = "invalid email address")
@Value("${email}")
private String email;
}
person.yaml
name: "test"
email: "abcdefg"
age: 11111
I use my Person class on SpringBootApplication with invalid email address and age, but it ran successfully without any validation exception.
But If I replace the @PropertySource(value = "classpath:person.yaml")
to @ConfigurationProperties(prefix = "person")
or use both @PropertySource
and @ConfigurationProperties
application.yaml
person:
name: "test"
email: "abcdefg"
age: 11111
My SpringBoot throw an validation exception successfully.