Commit f5445ba5 authored by Andy Wilkinson's avatar Andy Wilkinson

Remove OnValidatorAvailableCondition as it is now redundant

Previously, Hibernate Validator would fail to initialize if it was
on the classpath but an EL implementation was not.
OnValidatorAvailableCondition protected against this scenario by
initializing the validator.

The Hibernate Validator shortcoming was addressed in eb222209
(gh-7598). As a result, checking for the precences of the
ValidationProvider META-INF/services resource is now sufficient to
auto-configure validation. This commit removes
OnValidatorAvailableCondition as it is no longer necessary.

Closes gh-8110
parent 6b353fb0
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,24 +16,15 @@ ...@@ -16,24 +16,15 @@
package org.springframework.boot.autoconfigure.validation; package org.springframework.boot.autoconfigure.validation;
import javax.validation.Validation;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator; import javax.validation.executable.ExecutableValidator;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.validation.MessageInterpolatorFactory; import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
...@@ -46,7 +37,6 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess ...@@ -46,7 +37,6 @@ import org.springframework.validation.beanvalidation.MethodValidationPostProcess
*/ */
@ConditionalOnClass(ExecutableValidator.class) @ConditionalOnClass(ExecutableValidator.class)
@ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider") @ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider")
@Conditional(ValidationAutoConfiguration.OnValidatorAvailableCondition.class)
public class ValidationAutoConfiguration { public class ValidationAutoConfiguration {
@Bean @Bean
...@@ -67,23 +57,4 @@ public class ValidationAutoConfiguration { ...@@ -67,23 +57,4 @@ public class ValidationAutoConfiguration {
return processor; return processor;
} }
@Order(Ordered.LOWEST_PRECEDENCE)
static class OnValidatorAvailableCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage
.forCondition(getClass().getName());
try {
Validation.buildDefaultValidatorFactory().getValidator();
return ConditionOutcome.match(message.available("JSR-303 provider"));
}
catch (Exception ex) {
return ConditionOutcome.noMatch(message.notAvailable("JSR-303 provider"));
}
}
}
} }
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -49,12 +49,12 @@ public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests ...@@ -49,12 +49,12 @@ public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests
} }
@Test @Test
public void validationIsDisabled() { public void missingElDependencyIsTolerated() {
this.context = new AnnotationConfigApplicationContext( this.context = new AnnotationConfigApplicationContext(
ValidationAutoConfiguration.class); ValidationAutoConfiguration.class);
assertThat(this.context.getBeansOfType(Validator.class)).isEmpty(); assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)) assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class))
.isEmpty(); .hasSize(1);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment