W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們以 HttpSession 為例,通過(guò)實(shí)際例子向大家展示。如何通過(guò)Hasor Scope 實(shí)現(xiàn)一個(gè) HttpSession 作用域。
public class SessionScope implements Scope{
private static final ThreadLocal<HttpSession> session
= new ThreadLocal<HttpSession>();
public <T> Provider<T> scope(Object key, Provider<T> provider) {
HttpSession httpSession = session.get();
if (httpSession == null) {
return provider;
}
String keyStr = "session_scope_" + key.toString();
Object attribute = httpSession.getAttribute(keyStr);
Provider<T> finalProvider = provider;
if (attribute == null) {
httpSession.setAttribute(keyStr, provider);
} else {
finalProvider = (Provider<T>) httpSession.getAttribute(keyStr);
}
return finalProvider;
}
}
在例子中為了避免保存到 Session 中的 Bean 和本身 Session 中的數(shù)據(jù) key 出現(xiàn)沖突,我們特意加了一個(gè)前綴用于區(qū)分。
現(xiàn)在作用域的功能是有了,但是我們的 HttpSession 對(duì)象的還沒(méi)有做初始化。這次我們來(lái)實(shí)現(xiàn) Filter 接口,在每次 request 請(qǐng)求到來(lái)的時(shí)候把 Session 都更新到 ThreadLocal 中。在訪問(wèn)結(jié)束之后再把 ThreadLocal 清理掉。
下面來(lái)看改造了之后的 Scope 代碼:
public class SessionScope implements Scope, Filter {
private static final ThreadLocal<HttpSession> session
= new ThreadLocal<HttpSession>();
public void init(FilterConfig filterConfig) { ... }
public void destroy() { ... }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
if (session.get() != null) {
session.remove();
}
session.set(((HttpServletRequest) request).getSession(true));
chain.doFilter(request, response);
} finally {
if (session.get() != null) {
session.remove();
}
}
}
public <T> Provider<T> scope(Object key, Provider<T> provider) {
HttpSession httpSession = session.get();
if (httpSession == null) {
return provider;
}
String keyStr = "session_scope_" + key.toString();
Object attribute = httpSession.getAttribute(keyStr);
Provider<T> finalProvider = provider;
if (attribute == null) {
httpSession.setAttribute(keyStr, provider);
} else {
finalProvider = (Provider<T>) httpSession.getAttribute(keyStr);
}
return finalProvider;
}
}
從上面例子代碼中看到進(jìn)入 filter 時(shí)做了 Session 的初始化將其保存到 ThreadLocal ,離開(kāi)之后又把 ThreadLocal 清理掉。
最后我們?cè)?Hasor 初始化的時(shí)候把 Scope 配置到 Hasor 中:
public class StartModule extends WebModule {
public void loadModule(WebApiBinder apiBinder) throws Throwable {
...
SessionScope scope = new SessionScope();
apiBinder.filter("/*").through(0, scope);
apiBinder.registerScope("session", scope);
...
}
}
接下來(lái)使用這個(gè) Scope:
apiBinder.bindType(UserInfo.class).toScope(new SessionScope());
//or
apiBinder.bindType(UserInfo.class).toScope("session");
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: