Component uses Properties directly, performing type conversion and error checking as appropriate.
org.springframework.core.env.Environment is not a good approach, but it could be useful when the name of the property you need to access changes dynamically.
@Component @RequiredArgsConstructor public class Example { private Environment env; .... public void method(String pathPropertyName) { ..... String path = env.getProperty(pathPropertyName); ..... }
Component uses org.springframework.beans.factory.annotation.Value, which reads from properties and performs all type conversion and error checking
@Value("${security.enable-csrf:true}") private boolean csrfEnabled = true;
Other approach using property configuration.
@Scheduled( fixedRateString = "${runtime.participantParameters.heartBeatMs}", initialDelayString = "${runtime.participantParameters.heartBeatMs}") public void schedule() { }
@ConfigurationProperties can be used to map values from .properties( .yml also supported) to a POJO. It performs all type conversion and error checking using validation javax.validation.constraints
@Validated @Getter @Setter @ConfigurationProperties(prefix = "runtime") public class ClRuntimeParameterGroup { @Min(100) private long heartBeatMs; @Valid @Positive private long reportingTimeIntervalMs; @Valid @NotNull private ParticipantUpdateParameters updateParameters; @NotBlank private String description; }
Convert org.onap.policy.common.parameters.BeanValidationResult to Spring validator using org.onap.policy.common.parameters.validation.ParameterGroupConstraint
@NotNull @ParameterGroupConstraint private TopicParameterGroup topicParameterGroup;