Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
cf0bd0f9
Commit
cf0bd0f9
authored
Apr 01, 2021
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Fix no such bean definition with ancestor-defined Validator"
See gh-25800
parent
89581015
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
17 deletions
+36
-17
PrimaryDefaultValidatorPostProcessor.java
...gure/validation/PrimaryDefaultValidatorPostProcessor.java
+4
-17
ValidationAutoConfigurationTests.java
...onfigure/validation/ValidationAutoConfigurationTests.java
+32
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/PrimaryDefaultValidatorPostProcessor.java
View file @
cf0bd0f9
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
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.
...
...
@@ -19,8 +19,6 @@ package org.springframework.boot.autoconfigure.validation;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.beans.factory.BeanFactoryUtils
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
...
...
@@ -39,6 +37,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
*
* @author Stephane Nicoll
* @author Matej Nedic
* @author Andy Wilkinson
*/
class
PrimaryDefaultValidatorPostProcessor
implements
ImportBeanDefinitionRegistrar
,
BeanFactoryAware
{
...
...
@@ -80,10 +79,9 @@ class PrimaryDefaultValidatorPostProcessor implements ImportBeanDefinitionRegist
}
private
boolean
hasPrimarySpringValidator
()
{
String
[]
validatorBeans
=
BeanFactoryUtils
.
beanNamesForTypeIncludingAncestors
(
this
.
beanFactory
,
Validator
.
class
,
false
,
false
);
String
[]
validatorBeans
=
this
.
beanFactory
.
getBeanNamesForType
(
Validator
.
class
,
false
,
false
);
for
(
String
validatorBean
:
validatorBeans
)
{
BeanDefinition
definition
=
searchForBeanDefinition
(
this
.
beanFactory
,
validatorBean
);
BeanDefinition
definition
=
this
.
beanFactory
.
getBeanDefinition
(
validatorBean
);
if
(
definition
.
isPrimary
())
{
return
true
;
}
...
...
@@ -91,15 +89,4 @@ class PrimaryDefaultValidatorPostProcessor implements ImportBeanDefinitionRegist
return
false
;
}
private
BeanDefinition
searchForBeanDefinition
(
ConfigurableListableBeanFactory
clbf
,
String
validatorBean
)
{
if
(
clbf
.
containsLocalBean
(
validatorBean
))
{
return
clbf
.
getBeanDefinition
(
validatorBean
);
}
else
if
(
clbf
.
getParentBeanFactory
()
instanceof
ConfigurableListableBeanFactory
)
{
return
searchForBeanDefinition
((
ConfigurableListableBeanFactory
)
clbf
.
getParentBeanFactory
(),
validatorBean
);
}
throw
new
NoSuchBeanDefinitionException
(
validatorBean
);
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java
View file @
cf0bd0f9
...
...
@@ -26,6 +26,7 @@ import javax.validation.constraints.Size;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.BeanFactoryUtils
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
...
...
@@ -127,6 +128,37 @@ class ValidationAutoConfigurationTests {
});
}
@Test
void
whenUserProvidesSpringValidatorInParentContextThenAutoConfiguredValidatorIsPrimary
()
{
new
ApplicationContextRunner
().
withUserConfiguration
(
UserDefinedSpringValidatorConfig
.
class
).
run
((
parent
)
->
{
this
.
contextRunner
.
withParent
(
parent
).
run
((
context
)
->
{
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
containsExactly
(
"defaultValidator"
);
assertThat
(
BeanFactoryUtils
.
beanNamesForTypeIncludingAncestors
(
context
.
getBeanFactory
(),
org
.
springframework
.
validation
.
Validator
.
class
)).
containsExactly
(
"defaultValidator"
,
"customValidator"
,
"anotherCustomValidator"
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isTrue
();
});
});
}
@Test
void
whenUserProvidesPrimarySpringValidatorInParentContextThenAutoConfiguredValidatorIsPrimary
()
{
new
ApplicationContextRunner
().
withUserConfiguration
(
UserDefinedPrimarySpringValidatorConfig
.
class
)
.
run
((
parent
)
->
{
this
.
contextRunner
.
withParent
(
parent
).
run
((
context
)
->
{
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
containsExactly
(
"defaultValidator"
);
assertThat
(
BeanFactoryUtils
.
beanNamesForTypeIncludingAncestors
(
context
.
getBeanFactory
(),
org
.
springframework
.
validation
.
Validator
.
class
)).
containsExactly
(
"defaultValidator"
,
"customValidator"
,
"anotherCustomValidator"
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isTrue
();
});
});
}
@Test
void
validationIsEnabled
()
{
this
.
contextRunner
.
withUserConfiguration
(
SampleService
.
class
).
run
((
context
)
->
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment