Spring Boot Version:
1.5.6.RELEASE or 2.2.10.RELEASE
Problem Description:
When using a parent/child ApplicationContext setup, PrimaryDefaultValidatorPostProcessor may throw a NoSuchBeanDefinitionException.
The reason is that hasPrimarySpringValidator(...) mixes parent-aware and local-only lookups:
String[] validatorBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
this.beanFactory, Validator.class, false, false);
for (String validatorBean : validatorBeans) {
BeanDefinition definition = registry.getBeanDefinition(validatorBean); // local only
if (definition != null && definition.isPrimary()) {
return true;
}
}
beanNamesForTypeIncludingAncestors returns beans from the current and parent contexts.
But registry.getBeanDefinition only searches the current registry, not the parent.
If a Validator bean (e.g. mvcValidator) is defined in the parent context only, this mismatch causes:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mvcValidator' available
Steps to Reproduce:
-
Create a Spring Boot 1.5.6 project.
-
Define a mvcValidator bean in the parent ApplicationContext (e.g. via WebMvcConfigurationSupport).
-
Create a child ApplicationContext without mvcValidator.
-
Run the child with ValidationAutoConfiguration enabled.
-
Observe startup failure: NoSuchBeanDefinitionException.
Expected Behavior:
If beanNamesForTypeIncludingAncestors includes parent beans, then retrieving the BeanDefinition should also consider the parent. Alternatively, only local beans should be considered when checking for a primary Validator.
Workarounds:
-
Manually define a mvcValidator bean in the child context.
-
Exclude ValidationAutoConfiguration.
-
Override PrimaryDefaultValidatorPostProcessor.
Comment From: wilkinsona
Spring Boot 1.x and 2.x are no longer supported. Please upgrade to Spring Boot 3.4.x or later. If the problem still occurs and you would like us to re-open this issue to investigate, please create a minimal sample that reproduces the problem and share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: xiaolongzuo
@wilkinsona Thanks,I got.
Comment From: xiaolongzuo
@wilkinsona I found it. https://github.com/spring-projects/spring-boot/issues/25864