Polish
Closes gh-3534
This commit is contained in:
@@ -20,12 +20,18 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "sample")
|
||||
public class SampleProperties {
|
||||
|
||||
/**
|
||||
* Sample host.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
private Integer port;
|
||||
/**
|
||||
* Sample port.
|
||||
*/
|
||||
private Integer port = 8080;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
|
||||
@@ -16,46 +16,29 @@
|
||||
|
||||
package sample.propertyvalidation;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
public class SamplePropertiesValidator implements Validator {
|
||||
|
||||
@Component(value = "configurationPropertiesValidator")
|
||||
public class ConfigurationPropertiesValidator implements Validator {
|
||||
|
||||
public static final String IP_REGEX = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$";
|
||||
|
||||
final Pattern pattern = Pattern.compile(IP_REGEX);
|
||||
|
||||
private Set<String> validatedClasses = new HashSet<String>() {{
|
||||
add(SampleProperties.class.getName());
|
||||
}};
|
||||
final Pattern pattern = Pattern.compile("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$");
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> aClass) {
|
||||
return AnnotationUtils.findAnnotation(aClass, ConfigurationProperties.class) != null;
|
||||
public boolean supports(Class<?> type) {
|
||||
return type == SampleProperties.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(Object o, Errors errors) {
|
||||
if(validatedClasses.contains(o.getClass().getName())) {
|
||||
doValidation(o, errors);
|
||||
}
|
||||
}
|
||||
|
||||
private void doValidation(Object o, Errors errors) {
|
||||
ValidationUtils.rejectIfEmpty(errors, "host", "host.empty");
|
||||
ValidationUtils.rejectIfEmpty(errors, "port", "port.empty");
|
||||
|
||||
SampleProperties properties = (SampleProperties) o;
|
||||
if(!pattern.matcher(properties.getHost()).matches()) {
|
||||
if (properties.getHost() != null &&
|
||||
!pattern.matcher(properties.getHost()).matches()) {
|
||||
errors.rejectValue("host", "Invalid host");
|
||||
}
|
||||
}
|
||||
@@ -18,26 +18,42 @@ package sample.propertyvalidation;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties
|
||||
public class SamplePropertyValidationApplication implements CommandLineRunner {
|
||||
public class SamplePropertyValidationApplication {
|
||||
|
||||
@Autowired
|
||||
private SampleProperties properties;
|
||||
@Bean
|
||||
public Validator configurationPropertiesValidator() {
|
||||
return new SamplePropertiesValidator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
System.out.println("host: " + this.properties.getHost());
|
||||
System.out.println("port:" + this.properties.getPort());
|
||||
@Service
|
||||
@Profile("app")
|
||||
static class Startup implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private SampleProperties properties;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
System.out.println("=========================================");
|
||||
System.out.println("Sample host: " + this.properties.getHost());
|
||||
System.out.println("Sample port: " + this.properties.getPort());
|
||||
System.out.println("=========================================");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SamplePropertyValidationApplication.class, args);
|
||||
new SpringApplicationBuilder(SamplePropertyValidationApplication.class)
|
||||
.profiles("app").run(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user