|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Method;
|
20 | 20 | import java.util.ArrayList;
|
| 21 | +import java.util.Collections; |
21 | 22 | import java.util.LinkedHashMap;
|
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Map;
|
|
32 | 33 |
|
33 | 34 | import org.springframework.beans.factory.BeanFactory;
|
34 | 35 | import org.springframework.beans.factory.BeanFactoryAware;
|
| 36 | +import org.springframework.beans.factory.BeanFactoryUtils; |
35 | 37 | import org.springframework.beans.factory.InitializingBean;
|
| 38 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 39 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
36 | 40 | import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
37 | 41 | import org.springframework.core.DefaultParameterNameDiscoverer;
|
38 | 42 | import org.springframework.core.KotlinDetector;
|
|
41 | 45 | import org.springframework.core.ParameterNameDiscoverer;
|
42 | 46 | import org.springframework.core.ReactiveAdapterRegistry;
|
43 | 47 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
| 48 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
44 | 49 | import org.springframework.core.log.LogFormatUtils;
|
45 | 50 | import org.springframework.core.task.AsyncTaskExecutor;
|
46 | 51 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
|
96 | 101 | import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
|
97 | 102 | import org.springframework.web.method.support.InvocableHandlerMethod;
|
98 | 103 | import org.springframework.web.method.support.ModelAndViewContainer;
|
| 104 | +import org.springframework.web.servlet.DispatcherServlet; |
| 105 | +import org.springframework.web.servlet.LocaleResolver; |
99 | 106 | import org.springframework.web.servlet.ModelAndView;
|
100 | 107 | import org.springframework.web.servlet.View;
|
| 108 | +import org.springframework.web.servlet.ViewResolver; |
101 | 109 | import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
|
102 | 110 | import org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter;
|
103 | 111 | import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
@@ -767,7 +775,8 @@ private List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
|
767 | 775 | handlers.add(new ModelMethodProcessor());
|
768 | 776 | handlers.add(new ViewMethodReturnValueHandler());
|
769 | 777 | handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters(),
|
770 |
| - this.reactiveAdapterRegistry, this.taskExecutor, this.contentNegotiationManager)); |
| 778 | + this.reactiveAdapterRegistry, this.taskExecutor, this.contentNegotiationManager, |
| 779 | + initViewResolvers(), initLocaleResolver())); |
771 | 780 | handlers.add(new StreamingResponseBodyReturnValueHandler());
|
772 | 781 | handlers.add(new HttpEntityMethodProcessor(getMessageConverters(),
|
773 | 782 | this.contentNegotiationManager, this.requestResponseBodyAdvice, this.errorResponseInterceptors));
|
@@ -801,6 +810,33 @@ private List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
|
801 | 810 | return handlers;
|
802 | 811 | }
|
803 | 812 |
|
| 813 | + private List<ViewResolver> initViewResolvers() { |
| 814 | + if (getBeanFactory() instanceof ListableBeanFactory lbf) { |
| 815 | + Map<String, ViewResolver> matchingBeans = |
| 816 | + BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ViewResolver.class, true, false); |
| 817 | + if (!matchingBeans.isEmpty()) { |
| 818 | + List<ViewResolver> viewResolvers = new ArrayList<>(matchingBeans.values()); |
| 819 | + AnnotationAwareOrderComparator.sort(viewResolvers); |
| 820 | + return viewResolvers; |
| 821 | + } |
| 822 | + } |
| 823 | + return Collections.emptyList(); |
| 824 | + } |
| 825 | + |
| 826 | + @Nullable |
| 827 | + private LocaleResolver initLocaleResolver() { |
| 828 | + if (getBeanFactory() != null) { |
| 829 | + try { |
| 830 | + return getBeanFactory().getBean( |
| 831 | + DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class); |
| 832 | + } |
| 833 | + catch (NoSuchBeanDefinitionException ex) { |
| 834 | + // ignore |
| 835 | + } |
| 836 | + } |
| 837 | + return null; |
| 838 | + } |
| 839 | + |
804 | 840 | private static Predicate<MethodParameter> methodParamPredicate(
|
805 | 841 | List<HandlerMethodArgumentResolver> resolvers, Class<?> resolverType) {
|
806 | 842 |
|
|
0 commit comments