詳解Spring Controller autowired Request變量
詳解Spring Controller autowired Request變量
spring的DI大家比較熟悉了,對(duì)于依賴注入的實(shí)現(xiàn)也無須贅述。
那么spring的bean的默認(rèn)scope為singleton,對(duì)于controller來說每次方法中均可以獲得request還是比較有意思的。
對(duì)于方法參數(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. 對(duì)于controller等單實(shí)例變量來說如何動(dòng)態(tài)注入變量呢?spring使用了很聰明的辦法
- 首先request和用戶請(qǐng)求相關(guān)
- 不同的用戶同時(shí)訪問時(shí)是在不同的線程中
- 保存了用戶的請(qǐng)求在threadlocal中
- 用戶獲取該請(qǐng)求需要手動(dòng)調(diào)用threadlocal來獲取
- 為了幫助用戶減少重復(fù)代碼,spring可以讓用戶‘動(dòng)態(tài)'注入request
- 當(dāng)controller在實(shí)例化時(shí),動(dòng)態(tài)注冊(cè)一個(gè)proxy到當(dāng)前request變量中
- 此proxy當(dāng)被使用是可以將所有方法動(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ǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
jsp導(dǎo)出身份證到excel時(shí)候格式不對(duì)但以X結(jié)尾的卻可以
excel導(dǎo)出身份證的時(shí)候顯示有的對(duì)有的不對(duì),身份證以X結(jié)尾的可以,其它都顯示不正確,關(guān)于這個(gè)問題的解決方法如下,需要的朋友可以參考下2014-10-10Servlet網(wǎng)上售票問題引發(fā)線程安全問題的思考
這篇文章主要是關(guān)于Servlet模擬網(wǎng)上售票問題,引發(fā)的線程安全問題的思考,感興趣的小伙伴們可以參考一下2015-12-12Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)
這篇文章主要為大家詳細(xì)介紹了Jsp+Servlet實(shí)現(xiàn)文件上傳下載功能的第二部分,文件列表展示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Spring組件自動(dòng)掃描詳解及實(shí)例代碼
這篇文章主要介紹了Spring組件自動(dòng)掃描詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02