a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 編程 > JSP > 正文

詳解Spring Controller autowired Request變量

2024-09-05 00:23:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

詳解Spring Controller autowired Request變量

spring的DI大家比較熟悉了,對(duì)于依賴注入的實(shí)現(xiàn)也無(wú)須贅述。

那么spring的bean的默認(rèn)scope為singleton,對(duì)于controller來(lái)說(shuō)每次方法中均可以獲得request還是比較有意思的。

對(duì)于方法參數(shù)上的request通過(guò)構(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í)例變量來(lái)說(shuō)如何動(dòng)態(tài)注入變量呢?spring使用了很聰明的辦法

  1. 首先request和用戶請(qǐng)求相關(guān)
  2. 不同的用戶同時(shí)訪問(wèn)時(shí)是在不同的線程中
  3. 保存了用戶的請(qǐng)求在threadlocal中
  4. 用戶獲取該請(qǐng)求需要手動(dòng)調(diào)用threadlocal來(lái)獲取
  5. 為了幫助用戶減少重復(fù)代碼,spring可以讓用戶‘動(dòng)態(tài)'注入request
  6. 當(dāng)controller在實(shí)例化時(shí),動(dòng)態(tài)注冊(cè)一個(gè)proxy到當(dāng)前request變量中
  7. 此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"; } } 

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JSP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲精品无 | 国产一区视频在线 | 国产精品欧美日韩在线观看 | 亚洲伦理 | 欧美在线一二三 | 在线免费av观看 | 91免费看| 成人午夜av | 欧美日韩视频在线第一区 | 欧美成人一级 | 国产91亚洲精品 | 欧美午夜一区二区三区 | 狠狠做深爱婷婷综合一区 | 97国产一区二区 | 久久久999 | 中文字幕在线网址 | 希岛爱理在线 | 日韩一区二区三区在线 | 国产精品久久久久无码av | 五月天在线婷婷 | 精品日韩欧美一区二区三区在线播放 | 欧美国产精品一区 | 黄色毛片免费看 | 日韩视频在线观看视频 | 国产中文在线 | 久久久夜夜夜 | 成人精品在线 | 久久精品久久久久久久久久久久久 | 日韩毛片在线观看 | 精品国产区 | 国产高清在线观看 | www亚洲成人 | 国产极品美女在线 | 最新超碰 | 在线成人www免费观看视频 | 久久久久久网站 | 亚洲成人精品网 | 九九热精品视频 | 久久精彩视频 | 冷水浴在线观看 | 欧美日韩在线免费观看 |