mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
[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:
parent
504d96eb51
commit
61a94f212d
@ -89,16 +89,16 @@ public class ServerConfiguration implements BeanPostProcessor, BeanFactoryAware
|
|||||||
&& !this.initialized) {
|
&& !this.initialized) {
|
||||||
|
|
||||||
// Cannot use @Autowired because the injection happens too early
|
// Cannot use @Autowired because the injection happens too early
|
||||||
ServerProperties configuration = this.beanFactory
|
ServerProperties server = this.beanFactory
|
||||||
.getBean(ServerProperties.class);
|
.getBean(ServerProperties.class);
|
||||||
|
|
||||||
AbstractEmbeddedServletContainerFactory factory = (AbstractEmbeddedServletContainerFactory) bean;
|
AbstractEmbeddedServletContainerFactory factory = (AbstractEmbeddedServletContainerFactory) bean;
|
||||||
factory.setPort(configuration.getPort());
|
factory.setPort(server.getPort());
|
||||||
factory.setContextPath(configuration.getContextPath());
|
factory.setContextPath(server.getContextPath());
|
||||||
|
|
||||||
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
|
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
|
||||||
configureTomcat((TomcatEmbeddedServletContainerFactory) factory,
|
configureTomcat((TomcatEmbeddedServletContainerFactory) factory,
|
||||||
configuration);
|
server);
|
||||||
}
|
}
|
||||||
|
|
||||||
factory.setErrorPages(Collections
|
factory.setErrorPages(Collections
|
||||||
|
@ -41,7 +41,7 @@ abstract class AbstractOnBeanCondition implements Condition {
|
|||||||
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
|
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
|
||||||
annotationClass().getName(), true);
|
annotationClass().getName(), true);
|
||||||
List<String> beanClasses = collect(attributes, "value");
|
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,
|
Assert.isTrue(beanClasses.size() > 0 || beanNames.size() > 0,
|
||||||
"@" + ClassUtils.getShortName(annotationClass())
|
"@" + ClassUtils.getShortName(annotationClass())
|
||||||
+ " annotations must specify at least one bean");
|
+ " annotations must specify at least one bean");
|
||||||
|
@ -31,6 +31,7 @@ import org.springframework.core.env.MutablePropertySources;
|
|||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.env.PropertySources;
|
import org.springframework.core.env.PropertySources;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
import org.springframework.validation.Validator;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,6 +43,8 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class ConfigurationPropertiesBindingConfiguration {
|
public class ConfigurationPropertiesBindingConfiguration {
|
||||||
|
|
||||||
|
public final static String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private PropertySourcesPlaceholderConfigurer configurer;
|
private PropertySourcesPlaceholderConfigurer configurer;
|
||||||
|
|
||||||
@ -52,6 +55,17 @@ public class ConfigurationPropertiesBindingConfiguration {
|
|||||||
@Qualifier(ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME)
|
@Qualifier(ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME)
|
||||||
private ConversionService conversionService;
|
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
|
* Lifecycle hook that binds application properties to any bean whose type is
|
||||||
* decorated with {@link ConfigurationProperties} annotation.
|
* decorated with {@link ConfigurationProperties} annotation.
|
||||||
@ -74,9 +88,7 @@ public class ConfigurationPropertiesBindingConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
PropertySourcesBindingPostProcessor processor = new PropertySourcesBindingPostProcessor();
|
PropertySourcesBindingPostProcessor processor = new PropertySourcesBindingPostProcessor();
|
||||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
processor.setValidator(this.validator);
|
||||||
validator.afterPropertiesSet();
|
|
||||||
processor.setValidator(validator);
|
|
||||||
processor.setConversionService(this.conversionService);
|
processor.setConversionService(this.conversionService);
|
||||||
processor.setPropertySources(propertySources);
|
processor.setPropertySources(propertySources);
|
||||||
return processor;
|
return processor;
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -56,6 +56,8 @@ public class SimpleMainTests {
|
|||||||
public void xmlContext() throws Exception {
|
public void xmlContext() throws Exception {
|
||||||
Spring.main(new String[] { Spring.class.getName(),
|
Spring.main(new String[] { Spring.class.getName(),
|
||||||
"org/springframework/bootstrap/sample-beans.xml" });
|
"org/springframework/bootstrap/sample-beans.xml" });
|
||||||
|
this.context = Spring.getApplicationContext();
|
||||||
|
assertNotNull(this.context);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user