Remove internal configuration properties validator cleanup

This commit removes the cleanup of the javax.validator.Validator that
can be created to validate @ConfigurationProperties binding as the
memory effect can no longer be reproduced.

Closes gh-10573
This commit is contained in:
Stephane Nicoll 2017-10-24 10:05:52 +02:00
parent 59a906bd58
commit 76c4819053
5 changed files with 2 additions and 70 deletions

View File

@ -106,15 +106,6 @@ public class ConfigurationPropertiesBinder {
} }
} }
/**
* Destroy this binder instance.
*/
void destroy() {
if (this.validator instanceof InternalValidator) {
((InternalValidator) this.validator).destroy();
}
}
private Validator determineValidator(Object bean) { private Validator determineValidator(Object bean) {
boolean supportsBean = (this.validator != null boolean supportsBean = (this.validator != null
&& this.validator.supports(bean.getClass())); && this.validator.supports(bean.getClass()));
@ -154,16 +145,6 @@ public class ConfigurationPropertiesBinder {
return details.toString(); return details.toString();
} }
/**
* {@link Validator} extension to be implemented to signal that that validator can be
* destroyed once the binder is no longer in use.
*/
interface InternalValidator extends Validator {
void destroy();
}
/** /**
* {@link Validator} implementation that wraps {@link Validator} instances and chains * {@link Validator} implementation that wraps {@link Validator} instances and chains
* their execution. * their execution.

View File

@ -25,15 +25,12 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered; import org.springframework.core.PriorityOrdered;
@ -58,7 +55,7 @@ import org.springframework.validation.Validator;
*/ */
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor, public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor,
BeanFactoryAware, EnvironmentAware, ApplicationContextAware, InitializingBean, BeanFactoryAware, EnvironmentAware, ApplicationContextAware, InitializingBean,
DisposableBean, ApplicationListener<ContextRefreshedEvent>, PriorityOrdered { PriorityOrdered {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(ConfigurationPropertiesBindingPostProcessor.class); .getLog(ConfigurationPropertiesBindingPostProcessor.class);
@ -152,23 +149,6 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
} }
} }
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
freeBinder();
}
@Override
public void destroy() {
freeBinder();
}
private void freeBinder() {
if (this.configurationPropertiesBinder != null) {
this.configurationPropertiesBinder.destroy();
}
this.configurationPropertiesBinder = null;
}
private PropertySources deducePropertySources() { private PropertySources deducePropertySources() {
PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(); PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
if (configurer != null) { if (configurer != null) {

View File

@ -31,8 +31,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
class ValidatedLocalValidatorFactoryBean extends LocalValidatorFactoryBean class ValidatedLocalValidatorFactoryBean extends LocalValidatorFactoryBean {
implements ConfigurationPropertiesBinder.InternalValidator {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(ConfigurationPropertiesBindingPostProcessor.class); .getLog(ConfigurationPropertiesBindingPostProcessor.class);

View File

@ -22,7 +22,6 @@ import javax.validation.constraints.NotNull;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.context.properties.bind.validation.BindValidationException; import org.springframework.boot.context.properties.bind.validation.BindValidationException;
import org.springframework.boot.context.properties.bind.validation.ValidationErrors; import org.springframework.boot.context.properties.bind.validation.ValidationErrors;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
@ -39,8 +38,6 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link ConfigurationPropertiesBinderBuilder}. * Tests for {@link ConfigurationPropertiesBinderBuilder}.
@ -140,19 +137,6 @@ public class ConfigurationPropertiesBinderBuilderTests {
assertThat(target.getBar()).isEqualTo("654321"); assertThat(target.getBar()).isEqualTo("654321");
} }
@Test
public void internalValidatorIsClosed() throws Exception {
ConfigurationPropertiesBinder binder = this.builder
.withEnvironment(this.environment).build();
Object validator = ReflectionTestUtils.getField(binder, "validator");
assertThat(validator).isNotNull();
assertThat(validator).isInstanceOf(DisposableBean.class);
DisposableBean validatorSpy = spy((DisposableBean) validator);
ReflectionTestUtils.setField(binder, "validator", validatorSpy);
binder.destroy();
verify(validatorSpy).destroy();
}
private ValidationErrors bindWithValidationErrors( private ValidationErrors bindWithValidationErrors(
ConfigurationPropertiesBinder binder, Object target) { ConfigurationPropertiesBinder binder, Object target) {
try { try {

View File

@ -55,7 +55,6 @@ import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -87,17 +86,6 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
} }
} }
@Test
public void binderIsNullOutAfterContextRefresh() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class);
this.context.refresh();
ConfigurationPropertiesBindingPostProcessor bean = this.context
.getBean(ConfigurationPropertiesBindingPostProcessor.class);
assertThat(ReflectionTestUtils.getField(bean, "configurationPropertiesBinder"))
.isNull();
}
@Test @Test
public void bindToInterfaceBean() { public void bindToInterfaceBean() {
MockEnvironment env = new MockEnvironment(); MockEnvironment env = new MockEnvironment();