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