importcom.cmos.common.exception.GeneralException;importorg.springframework.boot.bind.PropertiesConfigurationFactory;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.core.convert.ConversionService;importorg.springframework.core.convert.support.DefaultConversionService;import org.springframework.core.env.*;importorg.springframework.validation.Validator;importorg.springframework.validation.beanvalidation.SpringValidatorAdapter;importjavax.validation.Validation;import staticorg.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation;/***@authorhujunzheng
* @create 2018-07-03 18:01
*
* 不强依赖ConfigurationProperties,进行配置注入
**/
public class RelaxedConfigurationBinder{private final PropertiesConfigurationFactoryfactory;publicRelaxedConfigurationBinder(T object) {this(new PropertiesConfigurationFactory<>(object));
}public RelaxedConfigurationBinder(Classtype) {this(new PropertiesConfigurationFactory<>(type));
}public static RelaxedConfigurationBinderwith(T object) {return new RelaxedConfigurationBinder<>(object);
}public static RelaxedConfigurationBinder with(Classtype) {return new RelaxedConfigurationBinder<>(type);
}public RelaxedConfigurationBinder(PropertiesConfigurationFactoryfactory) {this.factory =factory;
ConfigurationProperties properties= getMergedAnnotation(factory.getObjectType(), ConfigurationProperties.class);
javax.validation.Validator validator=Validation.buildDefaultValidatorFactory().getValidator();
factory.setValidator(newSpringValidatorAdapter(validator));
factory.setConversionService(newDefaultConversionService());if (null !=properties) {
factory.setIgnoreNestedProperties(properties.ignoreNestedProperties());
factory.setIgnoreInvalidFields(properties.ignoreInvalidFields());
factory.setIgnoreUnknownFields(properties.ignoreUnknownFields());
factory.setTargetName(properties.prefix());
factory.setExceptionIfInvalid(properties.exceptionIfInvalid());
}
}public RelaxedConfigurationBindersetTargetName(String targetName) {
factory.setTargetName(targetName);return this;
}public RelaxedConfigurationBinder setPropertySources(PropertySource>... propertySources) {
MutablePropertySources sources= newMutablePropertySources();for (PropertySource>propertySource : propertySources) {
sources.addLast(propertySource);
}
factory.setPropertySources(sources);return this;
}public RelaxedConfigurationBindersetPropertySources(Environment environment) {
factory.setPropertySources(((ConfigurableEnvironment) environment).getPropertySources());return this;
}public RelaxedConfigurationBindersetPropertySources(PropertySources propertySources) {
factory.setPropertySources(propertySources);return this;
}public RelaxedConfigurationBindersetConversionService(ConversionService conversionService) {
factory.setConversionService(conversionService);return this;
}public RelaxedConfigurationBindersetValidator(Validator validator) {
factory.setValidator(validator);return this;
}public RelaxedConfigurationBinder setResolvePlaceholders(booleanresolvePlaceholders) {
factory.setResolvePlaceholders(resolvePlaceholders);return this;
}public T doBind() throwsGeneralException {try{returnfactory.getObject();
}catch(Exception ex) {throw new GeneralException("配置绑定失败!", ex);
}
}
}