濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 詳解Spring Controller autowired Request變量

詳解Spring Controller autowired Request變量

熱門(mén)標(biāo)簽:電話(huà)機(jī)器人需要使用網(wǎng)絡(luò)嗎 外呼系統(tǒng)使用方法 給地圖標(biāo)注得傭金 自繪地圖標(biāo)注數(shù)據(jù) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 潤(rùn)滑油銷(xiāo)售電銷(xiāo)機(jī)器人 如何看懂地圖標(biāo)注點(diǎn) 電銷(xiāo)機(jī)器人免培訓(xùn) 海外圖書(shū)館地圖標(biāo)注點(diǎn)

詳解Spring Controller autowired Request變量

spring的DI大家比較熟悉了,對(duì)于依賴(lài)注入的實(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和用戶(hù)請(qǐng)求相關(guān)
  2. 不同的用戶(hù)同時(shí)訪(fǎng)問(wèn)時(shí)是在不同的線(xiàn)程中
  3. 保存了用戶(hù)的請(qǐng)求在threadlocal中
  4. 用戶(hù)獲取該請(qǐng)求需要手動(dòng)調(diào)用threadlocal來(lái)獲取
  5. 為了幫助用戶(hù)減少重復(fù)代碼,spring可以讓用戶(hù)‘動(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 ObjectFactoryServletRequest>, Serializable {

 public ServletRequest getObject() {
  return currentRequestAttributes().getRequest();
 }

 @Override
 public String toString() {
  return "Current HttpServletRequest";
 }
 }
 

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

您可能感興趣的文章:
  • 如何在springMVC的controller中獲取request
  • 關(guān)于Spring MVC在Controller層中注入request的坑詳解
  • Spring實(shí)現(xiàn)在非controller中獲取request對(duì)象

標(biāo)簽:黃石 樂(lè)山 內(nèi)江 廣州 南京 貸款邀約 銅川 大連

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Spring Controller autowired Request變量》,本文關(guān)鍵詞  詳解,Spring,Controller,autowired,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解Spring Controller autowired Request變量》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于詳解Spring Controller autowired Request變量的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    桂林市| 阿拉善盟| 体育| 秀山| 临洮县| 无锡市| 井研县| 黄陵县| 板桥市| 云安县| 贞丰县| 弥渡县| 定西市| 定州市| 漯河市| 汪清县| 姜堰市| 左贡县| 磴口县| 桦甸市| 佳木斯市| 安远县| 柘荣县| 资溪县| 鞍山市| 博爱县| 鸡东县| 西和县| 衢州市| 湖北省| 盱眙县| 温州市| 会昌县| 林甸县| 怀化市| 和林格尔县| 武隆县| 东台市| 长乐市| 恩施市| 砀山县|