[bs-89] Allow user to plugin validator for @ConfigurationProperties

* Also fix ordering problem in integration tests
(An application context not being closed led  to port 8080
being already in use.)
* Validator can be specified by providing a Spring Validator with
bean id configurationPropertiesValidator.

[Fixes #49067859]
This commit is contained in:
Dave Syer 2013-05-02 16:18:21 +01:00
parent 504d96eb51
commit 61a94f212d
5 changed files with 57 additions and 8 deletions

View File

@ -89,16 +89,16 @@ public class ServerConfiguration implements BeanPostProcessor, BeanFactoryAware
&& !this.initialized) {
// Cannot use @Autowired because the injection happens too early
ServerProperties configuration = this.beanFactory
ServerProperties server = this.beanFactory
.getBean(ServerProperties.class);
AbstractEmbeddedServletContainerFactory factory = (AbstractEmbeddedServletContainerFactory) bean;
factory.setPort(configuration.getPort());
factory.setContextPath(configuration.getContextPath());
factory.setPort(server.getPort());
factory.setContextPath(server.getContextPath());
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
configureTomcat((TomcatEmbeddedServletContainerFactory) factory,
configuration);
server);
}
factory.setErrorPages(Collections

View File

@ -41,7 +41,7 @@ abstract class AbstractOnBeanCondition implements Condition {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
annotationClass().getName(), true);
List<String> beanClasses = collect(attributes, "value");
List<String> beanNames = collect(attributes, "value");
List<String> beanNames = collect(attributes, "name");
Assert.isTrue(beanClasses.size() > 0 || beanNames.size() > 0,
"@" + ClassUtils.getShortName(annotationClass())
+ " annotations must specify at least one bean");

View File

@ -31,6 +31,7 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
/**
@ -42,6 +43,8 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@Configuration
public class ConfigurationPropertiesBindingConfiguration {
public final static String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
@Autowired(required = false)
private PropertySourcesPlaceholderConfigurer configurer;
@ -52,6 +55,17 @@ public class ConfigurationPropertiesBindingConfiguration {
@Qualifier(ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME)
private ConversionService conversionService;
@Autowired(required = false)
@Qualifier(VALIDATOR_BEAN_NAME)
private Validator validator;
@Bean
@ConditionalOnMissingBean(name = VALIDATOR_BEAN_NAME)
@ConditionalOnClass(name = "javax.validation.Validator")
protected Validator configurationPropertiesValidator() {
return new LocalValidatorFactoryBean();
}
/**
* Lifecycle hook that binds application properties to any bean whose type is
* decorated with {@link ConfigurationProperties} annotation.
@ -74,9 +88,7 @@ public class ConfigurationPropertiesBindingConfiguration {
}
}
PropertySourcesBindingPostProcessor processor = new PropertySourcesBindingPostProcessor();
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
processor.setValidator(validator);
processor.setValidator(this.validator);
processor.setConversionService(this.conversionService);
processor.setPropertySources(propertySources);
return processor;

View File

@ -0,0 +1,35 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactoryTests;
import org.springframework.bootstrap.main.SimpleMainTests;
/**
* A test suite for probing weird ordering problems in the tests.
* @author Dave Syer
*
*/
@RunWith(Suite.class)
@SuiteClasses({ SimpleMainTests.class, JettyEmbeddedServletContainerFactoryTests.class })
@Ignore
public class AdhocTestSuite {
}

View File

@ -56,6 +56,8 @@ public class SimpleMainTests {
public void xmlContext() throws Exception {
Spring.main(new String[] { Spring.class.getName(),
"org/springframework/bootstrap/sample-beans.xml" });
this.context = Spring.getApplicationContext();
assertNotNull(this.context);
}
}