18
18
19
19
import java .io .IOException ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Collections ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
23
24
import java .util .Properties ;
24
-
25
25
import javax .servlet .http .HttpServletRequest ;
26
26
import javax .servlet .http .HttpServletRequestWrapper ;
27
27
28
28
import org .springframework .beans .factory .BeanFactoryUtils ;
29
+ import org .springframework .beans .factory .InitializingBean ;
29
30
import org .springframework .context .ApplicationContext ;
31
+ import org .springframework .context .ApplicationContextAware ;
30
32
import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
31
33
import org .springframework .core .io .ClassPathResource ;
32
34
import org .springframework .core .io .Resource ;
33
35
import org .springframework .core .io .support .PropertiesLoaderUtils ;
34
36
import org .springframework .lang .Nullable ;
37
+ import org .springframework .util .Assert ;
35
38
import org .springframework .util .ClassUtils ;
36
39
import org .springframework .util .StringUtils ;
37
40
import org .springframework .web .cors .CorsConfiguration ;
56
59
* @author Rossen Stoyanchev
57
60
* @since 4.3.1
58
61
*/
59
- public class HandlerMappingIntrospector implements CorsConfigurationSource {
62
+ public class HandlerMappingIntrospector
63
+ implements CorsConfigurationSource , ApplicationContextAware , InitializingBean {
64
+
65
+ @ Nullable
66
+ private ApplicationContext applicationContext ;
67
+
68
+ @ Nullable
69
+ private List <HandlerMapping > handlerMappings ;
60
70
61
- private final List <HandlerMapping > handlerMappings ;
62
71
72
+ /**
73
+ * Constructor for use with {@link ApplicationContextAware}.
74
+ */
75
+ public HandlerMappingIntrospector () {
76
+ }
63
77
64
78
/**
65
79
* Constructor that detects the configured {@code HandlerMapping}s in the
66
80
* given {@code ApplicationContext} or falls back on
67
81
* "DispatcherServlet.properties" like the {@code DispatcherServlet}.
68
82
*/
83
+ @ Deprecated
69
84
public HandlerMappingIntrospector (ApplicationContext context ) {
70
85
this .handlerMappings = initHandlerMappings (context );
71
86
}
72
87
73
88
74
- private static List <HandlerMapping > initHandlerMappings (ApplicationContext context ) {
89
+ /**
90
+ * Return the configured HandlerMapping's.
91
+ */
92
+ public List <HandlerMapping > getHandlerMappings () {
93
+ return this .handlerMappings ;
94
+ }
95
+
96
+
97
+ @ Override
98
+ public void setApplicationContext (ApplicationContext applicationContext ) {
99
+ this .applicationContext = applicationContext ;
100
+ }
101
+
102
+ @ Override
103
+ public void afterPropertiesSet () {
104
+ if (this .handlerMappings == null ) {
105
+ Assert .notNull (this .applicationContext , "No ApplicationContext" );
106
+ this .handlerMappings = initHandlerMappings (this .applicationContext );
107
+ }
108
+ }
109
+
110
+ private static List <HandlerMapping > initHandlerMappings (ApplicationContext applicationContext ) {
75
111
Map <String , HandlerMapping > beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (
76
- context , HandlerMapping .class , true , false );
112
+ applicationContext , HandlerMapping .class , true , false );
77
113
if (!beans .isEmpty ()) {
78
114
List <HandlerMapping > mappings = new ArrayList <>(beans .values ());
79
115
AnnotationAwareOrderComparator .sort (mappings );
80
- return mappings ;
116
+ return Collections . unmodifiableList ( mappings ) ;
81
117
}
82
- return initDefaultHandlerMappings ( context );
118
+ return Collections . unmodifiableList ( initFallback ( applicationContext ) );
83
119
}
84
120
85
- private static List <HandlerMapping > initDefaultHandlerMappings (ApplicationContext context ) {
121
+ private static List <HandlerMapping > initFallback (ApplicationContext applicationContext ) {
86
122
Properties props ;
87
123
String path = "DispatcherServlet.properties" ;
88
124
try {
@@ -99,7 +135,7 @@ private static List<HandlerMapping> initDefaultHandlerMappings(ApplicationContex
99
135
for (String name : names ) {
100
136
try {
101
137
Class <?> clazz = ClassUtils .forName (name , DispatcherServlet .class .getClassLoader ());
102
- Object mapping = context .getAutowireCapableBeanFactory ().createBean (clazz );
138
+ Object mapping = applicationContext .getAutowireCapableBeanFactory ().createBean (clazz );
103
139
result .add ((HandlerMapping ) mapping );
104
140
}
105
141
catch (ClassNotFoundException ex ) {
@@ -110,13 +146,6 @@ private static List<HandlerMapping> initDefaultHandlerMappings(ApplicationContex
110
146
}
111
147
112
148
113
- /**
114
- * Return the configured HandlerMapping's.
115
- */
116
- public List <HandlerMapping > getHandlerMappings () {
117
- return this .handlerMappings ;
118
- }
119
-
120
149
/**
121
150
* Find the {@link HandlerMapping} that would handle the given request and
122
151
* return it as a {@link MatchableHandlerMapping} that can be used to test
@@ -129,6 +158,7 @@ public List<HandlerMapping> getHandlerMappings() {
129
158
*/
130
159
@ Nullable
131
160
public MatchableHandlerMapping getMatchableHandlerMapping (HttpServletRequest request ) throws Exception {
161
+ Assert .notNull (this .handlerMappings , "Handler mappings not initialized" );
132
162
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper (request );
133
163
for (HandlerMapping handlerMapping : this .handlerMappings ) {
134
164
Object handler = handlerMapping .getHandler (wrapper );
@@ -146,6 +176,7 @@ public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest req
146
176
@ Override
147
177
@ Nullable
148
178
public CorsConfiguration getCorsConfiguration (HttpServletRequest request ) {
179
+ Assert .notNull (this .handlerMappings , "Handler mappings not initialized" );
149
180
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper (request );
150
181
for (HandlerMapping handlerMapping : this .handlerMappings ) {
151
182
HandlerExecutionChain handler = null ;
0 commit comments