3232
3333import org .springframework .beans .BeansException ;
3434import org .springframework .beans .factory .BeanFactory ;
35+ import org .springframework .beans .factory .BeanFactoryAware ;
3536import org .springframework .beans .factory .BeanFactoryUtils ;
3637import org .springframework .beans .factory .DisposableBean ;
3738import org .springframework .beans .factory .InitializingBean ;
3839import org .springframework .beans .factory .ListableBeanFactory ;
3940import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
4041import org .springframework .beans .factory .ObjectProvider ;
4142import org .springframework .beans .factory .annotation .Autowired ;
42- import org .springframework .beans .factory .config .BeanDefinition ;
43- import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
44- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
4543import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
46- import org .springframework .beans .factory .support .BeanDefinitionRegistryPostProcessor ;
4744import org .springframework .beans .factory .support .RootBeanDefinition ;
4845import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
4946import org .springframework .boot .autoconfigure .AutoConfigureOrder ;
6865import org .springframework .context .annotation .Configuration ;
6966import org .springframework .context .annotation .ConfigurationCondition ;
7067import org .springframework .context .annotation .Import ;
68+ import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
7169import org .springframework .context .annotation .Primary ;
72- import org .springframework .context .annotation .Role ;
7370import org .springframework .core .Ordered ;
74- import org .springframework .core .annotation .Order ;
7571import org .springframework .core .convert .converter .Converter ;
7672import org .springframework .core .convert .converter .GenericConverter ;
7773import org .springframework .core .io .Resource ;
7874import org .springframework .core .type .AnnotatedTypeMetadata ;
75+ import org .springframework .core .type .AnnotationMetadata ;
7976import org .springframework .format .Formatter ;
8077import org .springframework .format .FormatterRegistry ;
8178import org .springframework .format .datetime .DateFormatter ;
@@ -163,12 +160,6 @@ public class WebMvcAutoConfiguration {
163160 public static final String SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE = PathExtensionContentNegotiationStrategy .class
164161 .getName () + ".SKIP" ;
165162
166- @ Bean
167- @ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
168- public static MvcValidatorPostProcessor mvcValidatorAliasPostProcessor () {
169- return new MvcValidatorPostProcessor ();
170- }
171-
172163 @ Bean
173164 @ ConditionalOnMissingBean (HiddenHttpMethodFilter .class )
174165 public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter () {
@@ -185,7 +176,7 @@ public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
185176 // Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
186177 // on the classpath
187178 @ Configuration
188- @ Import (EnableWebMvcConfiguration .class )
179+ @ Import ({ EnableWebMvcConfiguration .class , MvcValidatorRegistrar . class } )
189180 @ EnableConfigurationProperties ({ WebMvcProperties .class , ResourceProperties .class })
190181 public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
191182
@@ -642,7 +633,7 @@ public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
642633
643634 /**
644635 * Condition used to disable the default MVC validator registration. The
645- * {@link MvcValidatorPostProcessor } is used to configure the {@code mvcValidator}
636+ * {@link MvcValidatorRegistrar } is actually used to register the {@code mvcValidator}
646637 * bean.
647638 */
648639 static class DisableMvcValidatorCondition implements ConfigurationCondition {
@@ -660,8 +651,8 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
660651 }
661652
662653 /**
663- * {@link BeanFactoryPostProcessor } to deal with the MVC validator bean registration.
664- * Applies the following rules:
654+ * {@link ImportBeanDefinitionRegistrar } to deal with the MVC validator bean
655+ * registration. Applies the following rules:
665656 * <ul>
666657 * <li>With no validators - Uses standard
667658 * {@link WebMvcConfigurationSupport#mvcValidator()} logic.</li>
@@ -670,43 +661,45 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
670661 * defined.</li>
671662 * </ul>
672663 */
673- @ Order (Ordered .LOWEST_PRECEDENCE )
674- static class MvcValidatorPostProcessor
675- implements BeanDefinitionRegistryPostProcessor {
664+ static class MvcValidatorRegistrar
665+ implements ImportBeanDefinitionRegistrar , BeanFactoryAware {
676666
677667 private static final String JSR303_VALIDATOR_CLASS = "javax.validation.Validator" ;
678668
669+ private BeanFactory beanFactory ;
670+
679671 @ Override
680- public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry )
681- throws BeansException {
682- if (registry instanceof ListableBeanFactory ) {
683- postProcess (registry , (ListableBeanFactory ) registry );
684- }
672+ public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
673+ this .beanFactory = beanFactory ;
685674 }
686675
687676 @ Override
688- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory )
689- throws BeansException {
677+ public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata ,
678+ BeanDefinitionRegistry registry ) {
679+ if (this .beanFactory instanceof ListableBeanFactory ) {
680+ registerOrAliasMvcValidator (registry ,
681+ (ListableBeanFactory ) this .beanFactory );
682+ }
690683 }
691684
692- private void postProcess (BeanDefinitionRegistry registry ,
685+ private void registerOrAliasMvcValidator (BeanDefinitionRegistry registry ,
693686 ListableBeanFactory beanFactory ) {
694687 String [] validatorBeans = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
695688 beanFactory , Validator .class , false , false );
696689 if (validatorBeans .length == 0 ) {
697- registerMvcValidator (registry , beanFactory );
690+ registerNewMvcValidator (registry , beanFactory );
698691 }
699692 else if (validatorBeans .length == 1 ) {
700693 registry .registerAlias (validatorBeans [0 ], "mvcValidator" );
701694 }
702695 else {
703696 if (!ObjectUtils .containsElement (validatorBeans , "mvcValidator" )) {
704- registerMvcValidator (registry , beanFactory );
697+ registerNewMvcValidator (registry , beanFactory );
705698 }
706699 }
707700 }
708701
709- private void registerMvcValidator (BeanDefinitionRegistry registry ,
702+ private void registerNewMvcValidator (BeanDefinitionRegistry registry ,
710703 ListableBeanFactory beanFactory ) {
711704 RootBeanDefinition definition = new RootBeanDefinition ();
712705 definition .setBeanClass (getClass ());
0 commit comments