|
@@ -5,9 +5,9 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
|
public class WebThreadLocal {
|
|
|
|
|
|
- private static void checkRequestContext() {
|
|
|
+ private static RequestAttributes checkRequestContext() {
|
|
|
try {
|
|
|
- RequestContextHolder.currentRequestAttributes();
|
|
|
+ return RequestContextHolder.currentRequestAttributes();
|
|
|
} catch (IllegalStateException e) {
|
|
|
throw new IllegalStateException("必须在Web请求线程中调用!");
|
|
|
}
|
|
@@ -18,17 +18,12 @@ public class WebThreadLocal {
|
|
|
}
|
|
|
|
|
|
public static <T> void create(String name, T value) {
|
|
|
- checkRequestContext();
|
|
|
- RequestContextHolder
|
|
|
- .currentRequestAttributes()
|
|
|
- .setAttribute(getKeyName(name), value, RequestAttributes.SCOPE_REQUEST);
|
|
|
+ checkRequestContext().setAttribute(getKeyName(name), value, RequestAttributes.SCOPE_REQUEST);
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public static <T> T get(String name) {
|
|
|
- checkRequestContext();
|
|
|
- return (T) RequestContextHolder
|
|
|
- .currentRequestAttributes()
|
|
|
+ return (T) checkRequestContext()
|
|
|
.getAttribute(getKeyName(name), RequestAttributes.SCOPE_REQUEST);
|
|
|
}
|
|
|
}
|