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 ;
72
69
import org .springframework .context .annotation .Configuration ;
73
70
import org .springframework .context .annotation .ConfigurationCondition ;
74
71
import org .springframework .context .annotation .Import ;
72
+ import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
75
73
import org .springframework .context .annotation .Primary ;
76
- import org .springframework .context .annotation .Role ;
77
74
import org .springframework .core .Ordered ;
78
- import org .springframework .core .annotation .Order ;
79
75
import org .springframework .core .convert .converter .Converter ;
80
76
import org .springframework .core .convert .converter .GenericConverter ;
81
77
import org .springframework .core .io .Resource ;
82
78
import org .springframework .core .type .AnnotatedTypeMetadata ;
79
+ import org .springframework .core .type .AnnotationMetadata ;
83
80
import org .springframework .format .Formatter ;
84
81
import org .springframework .format .FormatterRegistry ;
85
82
import org .springframework .format .datetime .DateFormatter ;
@@ -160,19 +157,6 @@ public class WebMvcAutoConfiguration {
160
157
161
158
public static final String DEFAULT_SUFFIX = "" ;
162
159
163
- /**
164
- * Attribute that can be added to the web request when the
165
- * {@link PathExtensionContentNegotiationStrategy} should be be skipped.
166
- */
167
- public static final String SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE = PathExtensionContentNegotiationStrategy .class
168
- .getName () + ".SKIP" ;
169
-
170
- @ Bean
171
- @ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
172
- public static MvcValidatorPostProcessor mvcValidatorAliasPostProcessor () {
173
- return new MvcValidatorPostProcessor ();
174
- }
175
-
176
160
@ Bean
177
161
@ ConditionalOnMissingBean (HiddenHttpMethodFilter .class )
178
162
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter () {
@@ -189,7 +173,7 @@ public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
189
173
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
190
174
// on the classpath
191
175
@ Configuration
192
- @ Import (EnableWebMvcConfiguration .class )
176
+ @ Import ({ EnableWebMvcConfiguration .class , MvcValidatorRegistrar . class } )
193
177
@ EnableConfigurationProperties ({ WebMvcProperties .class , ResourceProperties .class })
194
178
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
195
179
@@ -623,6 +607,9 @@ private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) {
623
607
static class OptionalPathExtensionContentNegotiationStrategy
624
608
implements ContentNegotiationStrategy {
625
609
610
+ private static final String SKIP_ATTRIBUTE = PathExtensionContentNegotiationStrategy .class
611
+ .getName () + ".SKIP" ;
612
+
626
613
private final ContentNegotiationStrategy delegate ;
627
614
628
615
OptionalPathExtensionContentNegotiationStrategy (
@@ -633,8 +620,7 @@ static class OptionalPathExtensionContentNegotiationStrategy
633
620
@ Override
634
621
public List <MediaType > resolveMediaTypes (NativeWebRequest webRequest )
635
622
throws HttpMediaTypeNotAcceptableException {
636
- Object skip = webRequest .getAttribute (
637
- SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE ,
623
+ Object skip = webRequest .getAttribute (SKIP_ATTRIBUTE ,
638
624
RequestAttributes .SCOPE_REQUEST );
639
625
if (skip != null && Boolean .parseBoolean (skip .toString ())) {
640
626
return Collections .emptyList ();
@@ -646,7 +632,7 @@ public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
646
632
647
633
/**
648
634
* Condition used to disable the default MVC validator registration. The
649
- * {@link MvcValidatorPostProcessor } is used to configure the {@code mvcValidator}
635
+ * {@link MvcValidatorRegistrar } is actually used to register the {@code mvcValidator}
650
636
* bean.
651
637
*/
652
638
static class DisableMvcValidatorCondition implements ConfigurationCondition {
@@ -664,8 +650,8 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
664
650
}
665
651
666
652
/**
667
- * {@link BeanFactoryPostProcessor } to deal with the MVC validator bean registration.
668
- * Applies the following rules:
653
+ * {@link ImportBeanDefinitionRegistrar } to deal with the MVC validator bean
654
+ * registration. Applies the following rules:
669
655
* <ul>
670
656
* <li>With no validators - Uses standard
671
657
* {@link WebMvcConfigurationSupport#mvcValidator()} logic.</li>
@@ -674,43 +660,45 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
674
660
* defined.</li>
675
661
* </ul>
676
662
*/
677
- @ Order (Ordered .LOWEST_PRECEDENCE )
678
- static class MvcValidatorPostProcessor
679
- implements BeanDefinitionRegistryPostProcessor {
663
+ static class MvcValidatorRegistrar
664
+ implements ImportBeanDefinitionRegistrar , BeanFactoryAware {
680
665
681
666
private static final String JSR303_VALIDATOR_CLASS = "javax.validation.Validator" ;
682
667
668
+ private BeanFactory beanFactory ;
669
+
683
670
@ Override
684
- public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry )
685
- throws BeansException {
686
- if (registry instanceof ListableBeanFactory ) {
687
- postProcess (registry , (ListableBeanFactory ) registry );
688
- }
671
+ public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
672
+ this .beanFactory = beanFactory ;
689
673
}
690
674
691
675
@ Override
692
- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory )
693
- throws BeansException {
676
+ public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata ,
677
+ BeanDefinitionRegistry registry ) {
678
+ if (this .beanFactory instanceof ListableBeanFactory ) {
679
+ registerOrAliasMvcValidator (registry ,
680
+ (ListableBeanFactory ) this .beanFactory );
681
+ }
694
682
}
695
683
696
- private void postProcess (BeanDefinitionRegistry registry ,
684
+ private void registerOrAliasMvcValidator (BeanDefinitionRegistry registry ,
697
685
ListableBeanFactory beanFactory ) {
698
686
String [] validatorBeans = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
699
687
beanFactory , Validator .class , false , false );
700
688
if (validatorBeans .length == 0 ) {
701
- registerMvcValidator (registry , beanFactory );
689
+ registerNewMvcValidator (registry , beanFactory );
702
690
}
703
691
else if (validatorBeans .length == 1 ) {
704
692
registry .registerAlias (validatorBeans [0 ], "mvcValidator" );
705
693
}
706
694
else {
707
695
if (!ObjectUtils .containsElement (validatorBeans , "mvcValidator" )) {
708
- registerMvcValidator (registry , beanFactory );
696
+ registerNewMvcValidator (registry , beanFactory );
709
697
}
710
698
}
711
699
}
712
700
713
- private void registerMvcValidator (BeanDefinitionRegistry registry ,
701
+ private void registerNewMvcValidator (BeanDefinitionRegistry registry ,
714
702
ListableBeanFactory beanFactory ) {
715
703
RootBeanDefinition definition = new RootBeanDefinition ();
716
704
definition .setBeanClass (getClass ());
0 commit comments