1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
29
29
import org .apache .commons .logging .LogFactory ;
30
30
31
31
import org .springframework .beans .factory .BeanFactory ;
32
+ import org .springframework .context .MessageSource ;
33
+ import org .springframework .context .i18n .LocaleContextHolder ;
32
34
import org .springframework .core .BridgeMethodResolver ;
33
35
import org .springframework .core .MethodParameter ;
34
36
import org .springframework .core .ResolvableType ;
@@ -70,6 +72,9 @@ public class HandlerMethod {
70
72
@ Nullable
71
73
private final BeanFactory beanFactory ;
72
74
75
+ @ Nullable
76
+ private final MessageSource messageSource ;
77
+
73
78
private final Class <?> beanType ;
74
79
75
80
private final Method method ;
@@ -101,6 +106,7 @@ public HandlerMethod(Object bean, Method method) {
101
106
Assert .notNull (method , "Method is required" );
102
107
this .bean = bean ;
103
108
this .beanFactory = null ;
109
+ this .messageSource = null ;
104
110
this .beanType = ClassUtils .getUserClass (bean );
105
111
this .method = method ;
106
112
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
@@ -118,6 +124,7 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
118
124
Assert .notNull (methodName , "Method name is required" );
119
125
this .bean = bean ;
120
126
this .beanFactory = null ;
127
+ this .messageSource = null ;
121
128
this .beanType = ClassUtils .getUserClass (bean );
122
129
this .method = bean .getClass ().getMethod (methodName , parameterTypes );
123
130
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (this .method );
@@ -132,11 +139,23 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
132
139
* re-create the {@code HandlerMethod} with an initialized bean.
133
140
*/
134
141
public HandlerMethod (String beanName , BeanFactory beanFactory , Method method ) {
142
+ this (beanName , beanFactory , null , method );
143
+ }
144
+
145
+ /**
146
+ * Variant of {@link #HandlerMethod(String, BeanFactory, Method)} that
147
+ * also accepts a {@link MessageSource}.
148
+ */
149
+ public HandlerMethod (
150
+ String beanName , BeanFactory beanFactory ,
151
+ @ Nullable MessageSource messageSource , Method method ) {
152
+
135
153
Assert .hasText (beanName , "Bean name is required" );
136
154
Assert .notNull (beanFactory , "BeanFactory is required" );
137
155
Assert .notNull (method , "Method is required" );
138
156
this .bean = beanName ;
139
157
this .beanFactory = beanFactory ;
158
+ this .messageSource = messageSource ;
140
159
Class <?> beanType = beanFactory .getType (beanName );
141
160
if (beanType == null ) {
142
161
throw new IllegalStateException ("Cannot resolve bean type for bean with name '" + beanName + "'" );
@@ -156,6 +175,7 @@ protected HandlerMethod(HandlerMethod handlerMethod) {
156
175
Assert .notNull (handlerMethod , "HandlerMethod is required" );
157
176
this .bean = handlerMethod .bean ;
158
177
this .beanFactory = handlerMethod .beanFactory ;
178
+ this .messageSource = handlerMethod .messageSource ;
159
179
this .beanType = handlerMethod .beanType ;
160
180
this .method = handlerMethod .method ;
161
181
this .bridgedMethod = handlerMethod .bridgedMethod ;
@@ -174,6 +194,7 @@ private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
174
194
Assert .notNull (handler , "Handler object is required" );
175
195
this .bean = handler ;
176
196
this .beanFactory = handlerMethod .beanFactory ;
197
+ this .messageSource = handlerMethod .messageSource ;
177
198
this .beanType = handlerMethod .beanType ;
178
199
this .method = handlerMethod .method ;
179
200
this .bridgedMethod = handlerMethod .bridgedMethod ;
@@ -199,8 +220,13 @@ private void evaluateResponseStatus() {
199
220
annotation = AnnotatedElementUtils .findMergedAnnotation (getBeanType (), ResponseStatus .class );
200
221
}
201
222
if (annotation != null ) {
223
+ String reason = annotation .reason ();
224
+ String resolvedReason = (StringUtils .hasText (reason ) && this .messageSource != null ?
225
+ this .messageSource .getMessage (reason , null , reason , LocaleContextHolder .getLocale ()) :
226
+ reason );
227
+
202
228
this .responseStatus = annotation .code ();
203
- this .responseStatusReason = annotation . reason () ;
229
+ this .responseStatusReason = resolvedReason ;
204
230
}
205
231
}
206
232
0 commit comments