|
32 | 32 | import org.slf4j.Logger; |
33 | 33 | import org.slf4j.LoggerFactory; |
34 | 34 |
|
| 35 | +import org.springframework.beans.BeansException; |
| 36 | +import org.springframework.beans.factory.InitializingBean; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.ApplicationContextAware; |
35 | 39 | import org.springframework.core.convert.TypeDescriptor; |
36 | 40 | import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair; |
37 | 41 | import org.springframework.core.convert.support.GenericConversionService; |
38 | 42 | import org.springframework.format.support.DefaultFormattingConversionService; |
| 43 | +import org.springframework.format.support.FormattingConversionService; |
39 | 44 | import org.springframework.lang.Nullable; |
| 45 | +import org.springframework.util.ClassUtils; |
| 46 | + |
40 | 47 |
|
41 | 48 | /** |
42 | 49 | * The type Web conversion service provider. |
43 | 50 | * @author bnasslashen |
44 | 51 | */ |
45 | | -public class WebConversionServiceProvider { |
| 52 | +public class WebConversionServiceProvider implements InitializingBean, ApplicationContextAware { |
46 | 53 |
|
| 54 | + /** |
| 55 | + * The constant CONVERTERS. |
| 56 | + */ |
47 | 57 | private static final String CONVERTERS = "converters"; |
48 | 58 |
|
49 | 59 | /** |
50 | 60 | * The constant LOGGER. |
51 | 61 | */ |
52 | 62 | private static final Logger LOGGER = LoggerFactory.getLogger(WebConversionServiceProvider.class); |
| 63 | + |
53 | 64 | /** |
54 | 65 | * The Formatting conversion service. |
55 | 66 | */ |
56 | | - private final GenericConversionService formattingConversionService; |
| 67 | + private GenericConversionService formattingConversionService; |
57 | 68 |
|
58 | 69 | /** |
59 | | - * Instantiates a new Web conversion service provider. |
60 | | - * |
61 | | - * @param webConversionServiceOptional the web conversion service optional |
| 70 | + * The Application context. |
| 71 | + */ |
| 72 | + private ApplicationContext applicationContext; |
| 73 | + |
| 74 | + /** |
| 75 | + * The constant SERVLET_APPLICATION_CONTEXT_CLASS. |
| 76 | + */ |
| 77 | + private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext"; |
| 78 | + |
| 79 | + /** |
| 80 | + * The constant REACTIVE_APPLICATION_CONTEXT_CLASS. |
62 | 81 | */ |
63 | | - public WebConversionServiceProvider(Optional<GenericConversionService> webConversionServiceOptional) { |
64 | | - if (webConversionServiceOptional.isPresent()) |
65 | | - this.formattingConversionService = webConversionServiceOptional.get(); |
| 82 | + private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext"; |
| 83 | + |
| 84 | + @Override |
| 85 | + public void afterPropertiesSet() { |
| 86 | + if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) { |
| 87 | + this.formattingConversionService = applicationContext.getBean("mvcConversionService", FormattingConversionService.class); |
| 88 | + } |
| 89 | + else if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) { |
| 90 | + this.formattingConversionService = applicationContext.getBean("webFluxConversionService", FormattingConversionService.class); |
| 91 | + } |
66 | 92 | else |
67 | 93 | formattingConversionService = new DefaultFormattingConversionService(); |
68 | 94 | } |
@@ -106,4 +132,25 @@ public Class<?> getSpringConvertedType(Class<?> clazz) { |
106 | 132 | } |
107 | 133 | return result; |
108 | 134 | } |
| 135 | + |
| 136 | + /** |
| 137 | + * Is assignable boolean. |
| 138 | + * |
| 139 | + * @param target the target |
| 140 | + * @param type the type |
| 141 | + * @return the boolean |
| 142 | + */ |
| 143 | + private boolean isAssignable(String target, Class<?> type) { |
| 144 | + try { |
| 145 | + return ClassUtils.resolveClassName(target, null).isAssignableFrom(type); |
| 146 | + } |
| 147 | + catch (Throwable ex) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + } |
| 151 | + @Override |
| 152 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 153 | + this.applicationContext = applicationContext; |
| 154 | + } |
| 155 | + |
109 | 156 | } |
0 commit comments