詳解Spring Controller autowired Request變量
詳解Spring Controller autowired Request變量
spring的DI大家比較熟悉了,對于依賴注入的實(shí)現(xiàn)也無須贅述。
那么spring的bean的默認(rèn)scope為singleton,對于controller來說每次方法中均可以獲得request還是比較有意思的。
對于方法參數(shù)上的request通過構(gòu)建方法的參數(shù)可以獲得最新的request
public final Object invokeForRequest(NativeWebRequest request, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
if (logger.isTraceEnabled()) {
StringBuilder sb = new StringBuilder("Invoking [");
sb.append(getBeanType().getSimpleName()).append(".");
sb.append(getMethod().getName()).append("] method with arguments ");
sb.append(Arrays.asList(args));
logger.trace(sb.toString());
}
Object returnValue = invoke(args);
if (logger.isTraceEnabled()) {
logger.trace("Method [" + getMethod().getName() + "] returned [" + returnValue + "]");
}
return returnValue;
}
2. 對于controller等單實(shí)例變量來說如何動態(tài)注入變量呢?spring使用了很聰明的辦法
- 首先request和用戶請求相關(guān)
- 不同的用戶同時訪問時是在不同的線程中
- 保存了用戶的請求在threadlocal中
- 用戶獲取該請求需要手動調(diào)用threadlocal來獲取
- 為了幫助用戶減少重復(fù)代碼,spring可以讓用戶‘動態(tài)'注入request
- 當(dāng)controller在實(shí)例化時,動態(tài)注冊一個proxy到當(dāng)前request變量中
- 此proxy當(dāng)被使用是可以將所有方法動態(tài)路由到threadlocal中該request變量上執(zhí)行
/**
* Register web-specific scopes ("request", "session", "globalSession", "application")
* with the given BeanFactory, as used by the WebApplicationContext.
* @param beanFactory the BeanFactory to configure
* @param sc the ServletContext that we're running within
*/
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc) {
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false));
beanFactory.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION, new SessionScope(true));
if (sc != null) {
ServletContextScope appScope = new ServletContextScope(sc);
beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
sc.setAttribute(ServletContextScope.class.getName(), appScope);
}
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
if (jsfPresent) {
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
}
}
/**
* Factory that exposes the current request object on demand.
*/
@SuppressWarnings("serial")
private static class RequestObjectFactory implements ObjectFactory<ServletRequest>, Serializable {
public ServletRequest getObject() {
return currentRequestAttributes().getRequest();
}
@Override
public String toString() {
return "Current HttpServletRequest";
}
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
jsp中如何實(shí)現(xiàn)按下回車鍵自動提交表單
當(dāng)提交form表單數(shù)據(jù)時直接觸發(fā)回車鍵,就可以提交表單。為了省事很多時候希望可以按回車鍵來提交表單,要控制這些行為,可以借助JS來達(dá)到要求。2016-10-10
Java Web開發(fā)之圖形驗(yàn)證碼的生成與使用方法
這篇文章主要介紹了Java Web開發(fā)之圖形驗(yàn)證碼的生成與使用方法,較為詳細(xì)的分析了JSP驗(yàn)證碼的實(shí)現(xiàn)原理與生成技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-09-09
實(shí)例講解JSP獲取ResultSet結(jié)果集中的數(shù)據(jù)的方法
這篇文章主要介紹了JSP獲取ResultSet結(jié)果集中數(shù)據(jù)的方法,文后還介紹一種遍歷ResultSet中的數(shù)據(jù)并轉(zhuǎn)化為表格的方法,需要的朋友可以參考下2016-04-04
JSP+Servlet制作Java Web登錄功能的全流程解析
Web頁面登陸功能包括前端的界面以及后臺的數(shù)據(jù)庫寫入,這里我們總結(jié)了JSP+Servlet制作Java Web登陸功能的全流程解析,需要的朋友可以參考下2016-05-05
使用jsp:include控制動態(tài)內(nèi)容的方法
這篇文章主要介紹了使用jsp:include控制動態(tài)內(nèi)容的方法,結(jié)合實(shí)例較為詳細(xì)的分析了JSP中include偽指令的功能、定義及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Cookie的使用及保存中文并用Cookie實(shí)現(xiàn)購物車功能
Cookie是服務(wù)器存放在客戶端的一些數(shù)據(jù),比如密碼。下面為大家介紹下使用Cookie保存中文并用Cookie實(shí)現(xiàn)購物車功能,喜歡的朋友可以學(xué)習(xí)下2013-08-08

