32
32
33
33
import org .springframework .beans .BeansException ;
34
34
import org .springframework .beans .factory .BeanFactory ;
35
+ import org .springframework .beans .factory .BeanFactoryAware ;
35
36
import org .springframework .beans .factory .BeanFactoryUtils ;
36
37
import org .springframework .beans .factory .DisposableBean ;
37
38
import org .springframework .beans .factory .InitializingBean ;
38
39
import org .springframework .beans .factory .ListableBeanFactory ;
39
40
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
40
41
import org .springframework .beans .factory .ObjectProvider ;
41
42
import 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 ;
45
43
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
46
- import org .springframework .beans .factory .support .BeanDefinitionRegistryPostProcessor ;
47
44
import org .springframework .beans .factory .support .RootBeanDefinition ;
48
45
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
49
46
import org .springframework .boot .autoconfigure .AutoConfigureOrder ;
68
65
import org .springframework .context .annotation .Configuration ;
69
66
import org .springframework .context .annotation .ConfigurationCondition ;
70
67
import org .springframework .context .annotation .Import ;
68
+ import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
71
69
import org .springframework .context .annotation .Primary ;
72
- import org .springframework .context .annotation .Role ;
73
70
import org .springframework .core .Ordered ;
74
- import org .springframework .core .annotation .Order ;
75
71
import org .springframework .core .convert .converter .Converter ;
76
72
import org .springframework .core .convert .converter .GenericConverter ;
77
73
import org .springframework .core .io .Resource ;
78
74
import org .springframework .core .type .AnnotatedTypeMetadata ;
75
+ import org .springframework .core .type .AnnotationMetadata ;
79
76
import org .springframework .format .Formatter ;
80
77
import org .springframework .format .FormatterRegistry ;
81
78
import org .springframework .format .datetime .DateFormatter ;
@@ -163,12 +160,6 @@ public class WebMvcAutoConfiguration {
163
160
public static final String SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE = PathExtensionContentNegotiationStrategy .class
164
161
.getName () + ".SKIP" ;
165
162
166
- @ Bean
167
- @ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
168
- public static MvcValidatorPostProcessor mvcValidatorAliasPostProcessor () {
169
- return new MvcValidatorPostProcessor ();
170
- }
171
-
172
163
@ Bean
173
164
@ ConditionalOnMissingBean (HiddenHttpMethodFilter .class )
174
165
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter () {
@@ -185,7 +176,7 @@ public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
185
176
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
186
177
// on the classpath
187
178
@ Configuration
188
- @ Import (EnableWebMvcConfiguration .class )
179
+ @ Import ({ EnableWebMvcConfiguration .class , MvcValidatorRegistrar . class } )
189
180
@ EnableConfigurationProperties ({ WebMvcProperties .class , ResourceProperties .class })
190
181
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
191
182
@@ -642,7 +633,7 @@ public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
642
633
643
634
/**
644
635
* 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}
646
637
* bean.
647
638
*/
648
639
static class DisableMvcValidatorCondition implements ConfigurationCondition {
@@ -660,8 +651,8 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
660
651
}
661
652
662
653
/**
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:
665
656
* <ul>
666
657
* <li>With no validators - Uses standard
667
658
* {@link WebMvcConfigurationSupport#mvcValidator()} logic.</li>
@@ -670,43 +661,45 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
670
661
* defined.</li>
671
662
* </ul>
672
663
*/
673
- @ Order (Ordered .LOWEST_PRECEDENCE )
674
- static class MvcValidatorPostProcessor
675
- implements BeanDefinitionRegistryPostProcessor {
664
+ static class MvcValidatorRegistrar
665
+ implements ImportBeanDefinitionRegistrar , BeanFactoryAware {
676
666
677
667
private static final String JSR303_VALIDATOR_CLASS = "javax.validation.Validator" ;
678
668
669
+ private BeanFactory beanFactory ;
670
+
679
671
@ 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 ;
685
674
}
686
675
687
676
@ 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
+ }
690
683
}
691
684
692
- private void postProcess (BeanDefinitionRegistry registry ,
685
+ private void registerOrAliasMvcValidator (BeanDefinitionRegistry registry ,
693
686
ListableBeanFactory beanFactory ) {
694
687
String [] validatorBeans = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
695
688
beanFactory , Validator .class , false , false );
696
689
if (validatorBeans .length == 0 ) {
697
- registerMvcValidator (registry , beanFactory );
690
+ registerNewMvcValidator (registry , beanFactory );
698
691
}
699
692
else if (validatorBeans .length == 1 ) {
700
693
registry .registerAlias (validatorBeans [0 ], "mvcValidator" );
701
694
}
702
695
else {
703
696
if (!ObjectUtils .containsElement (validatorBeans , "mvcValidator" )) {
704
- registerMvcValidator (registry , beanFactory );
697
+ registerNewMvcValidator (registry , beanFactory );
705
698
}
706
699
}
707
700
}
708
701
709
- private void registerMvcValidator (BeanDefinitionRegistry registry ,
702
+ private void registerNewMvcValidator (BeanDefinitionRegistry registry ,
710
703
ListableBeanFactory beanFactory ) {
711
704
RootBeanDefinition definition = new RootBeanDefinition ();
712
705
definition .setBeanClass (getClass ());
0 commit comments