How to make mandatory property injection via @Value annotation?

I have the following POJO class Test.java, which is populated from a properties file using annotation @ConfigurationProperties. I saw the use of annotations @Requiredto make it mandatory.

Instead of defining annotations at the level of the setter method, are there any annotations or parameters in the annotations @Valuethat I can use to define conditions like Mandatory, NotNull, etc.

@Component
@ConfigurationProperties(prefix = "com.test")
public class Test {
  private String name;

  @Required
  public void setName(String name) {
    name = name;
  }

  public String getName(String name) {
    name = name;
  }
}

Is this the right and only way to make a particular attribute mandatory? What are other such annotations that I could use for such conditions or verification purposes?

+4
source share
2 answers

, , . (, @NotNull ..) ( ).

@NotNull
@Size(min=2, max=10)
private String name;

, , , JSR303 . Spring Boot

+3

Spring docs ( 4.1.6.RELEASE), Value , Value, . Spring EL, , -nullity.

, @ConfigurationProperties, @Value.

, get/set Java , "com.test" + getName()/setName() com.test.name=... , @Value, Spring, .

@Value / , , . @Value("${com.test.name}") , @PropertySource, , com.test.name=...

, : http://blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.html http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html

+1

All Articles