4d698ee0e40e8e0c7837239472bdbad83844d70b
[JavaForFun] /
1 package de.spring.example.context;
2
3 import javax.inject.Named;
4
5 import org.springframework.util.Assert;
6
7 @Named("userNameThreadContext")
8 public class UsernameThreadContext {
9         public static final String USERNAME_HEADER = "USERNAME";
10         
11         private final ThreadLocal<String> contextHolder = new ThreadLocal<>();
12         
13         public void setUsername(String username) {
14                 Assert.notNull(username);
15                 
16                 contextHolder.set(username);
17         }
18         
19         public String getUsername() {
20                 return contextHolder.get();
21         }
22         
23         public void clearUsername() {
24                 contextHolder.remove();
25         }
26 }