|
|
|
@ -17,7 +17,6 @@ |
|
|
|
package org.springframework.boot.context.properties; |
|
|
|
package org.springframework.boot.context.properties; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.factory.BeanClassLoaderAware; |
|
|
|
|
|
|
|
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; |
|
|
|
@ -62,12 +61,13 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
|
|
|
* @author Phillip Webb |
|
|
|
* @author Phillip Webb |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor, |
|
|
|
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor, |
|
|
|
BeanFactoryAware, ResourceLoaderAware, EnvironmentAware, BeanClassLoaderAware, |
|
|
|
BeanFactoryAware, ResourceLoaderAware, EnvironmentAware, ApplicationContextAware, |
|
|
|
ApplicationContextAware, InitializingBean, DisposableBean { |
|
|
|
InitializingBean, DisposableBean { |
|
|
|
|
|
|
|
|
|
|
|
public static final String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator"; |
|
|
|
public static final String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String VALIDATOR_CLASS = "javax.validation.Validator"; |
|
|
|
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator", |
|
|
|
|
|
|
|
"javax.validation.ValidatorFactory" }; |
|
|
|
|
|
|
|
|
|
|
|
private PropertySources propertySources; |
|
|
|
private PropertySources propertySources; |
|
|
|
|
|
|
|
|
|
|
|
@ -87,8 +87,6 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc |
|
|
|
|
|
|
|
|
|
|
|
private Environment environment = new StandardEnvironment(); |
|
|
|
private Environment environment = new StandardEnvironment(); |
|
|
|
|
|
|
|
|
|
|
|
private ClassLoader beanClassLoader; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ApplicationContext applicationContext; |
|
|
|
private ApplicationContext applicationContext; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -127,11 +125,6 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc |
|
|
|
this.environment = environment; |
|
|
|
this.environment = environment; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void setBeanClassLoader(ClassLoader classLoader) { |
|
|
|
|
|
|
|
this.beanClassLoader = classLoader; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) { |
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) { |
|
|
|
this.applicationContext = applicationContext; |
|
|
|
this.applicationContext = applicationContext; |
|
|
|
@ -146,14 +139,9 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc |
|
|
|
|
|
|
|
|
|
|
|
if (this.validator == null) { |
|
|
|
if (this.validator == null) { |
|
|
|
this.validator = getOptionalBean(VALIDATOR_BEAN_NAME, Validator.class); |
|
|
|
this.validator = getOptionalBean(VALIDATOR_BEAN_NAME, Validator.class); |
|
|
|
if (this.validator == null |
|
|
|
if (this.validator == null && isJsr303Present()) { |
|
|
|
&& ClassUtils.isPresent(VALIDATOR_CLASS, this.beanClassLoader)) { |
|
|
|
this.validator = new Jsr303ValidatorFactory() |
|
|
|
LocalValidatorFactoryBean validatorToUse = (LocalValidatorFactoryBean) ClassUtils |
|
|
|
.run(this.applicationContext); |
|
|
|
.forName(LocalValidatorFactoryBean.class.getName(), |
|
|
|
|
|
|
|
this.beanClassLoader).newInstance(); |
|
|
|
|
|
|
|
validatorToUse.setApplicationContext(this.applicationContext); |
|
|
|
|
|
|
|
validatorToUse.afterPropertiesSet(); |
|
|
|
|
|
|
|
this.validator = validatorToUse; |
|
|
|
|
|
|
|
this.ownedValidator = true; |
|
|
|
this.ownedValidator = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -165,6 +153,15 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isJsr303Present() { |
|
|
|
|
|
|
|
for (String validatorClass : VALIDATOR_CLASSES) { |
|
|
|
|
|
|
|
if (!ClassUtils.isPresent(validatorClass, null)) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void destroy() throws Exception { |
|
|
|
public void destroy() throws Exception { |
|
|
|
if (this.ownedValidator) { |
|
|
|
if (this.ownedValidator) { |
|
|
|
@ -331,4 +328,19 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc |
|
|
|
return this.defaultConversionService; |
|
|
|
return this.defaultConversionService; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Factory to create JSR 303 LocalValidatorFactoryBean. Inner class to prevent class
|
|
|
|
|
|
|
|
* loader issues. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static class Jsr303ValidatorFactory { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Validator run(ApplicationContext applicationContext) { |
|
|
|
|
|
|
|
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); |
|
|
|
|
|
|
|
validator.setApplicationContext(applicationContext); |
|
|
|
|
|
|
|
validator.afterPropertiesSet(); |
|
|
|
|
|
|
|
return validator; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|