最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Controller autowired Request變量

 更新時間:2017年08月21日 16:55:19   投稿:lqh  
這篇文章主要介紹了詳解Spring Controller autowired Request變量的相關(guān)資料,通過此文希望能幫助到大家,需要的朋友可以參考下

詳解Spring Controller autowired Request變量

spring的DI大家比較熟悉了,對于依賴注入的實現(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等單實例變量來說如何動態(tài)注入變量呢?spring使用了很聰明的辦法

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

最新評論

湖北省| 芜湖县| 元江| 临湘市| 南召县| 徐州市| 天镇县| 铜梁县| 永仁县| 麦盖提县| 潼关县| 嘉兴市| 莲花县| 邹城市| 堆龙德庆县| 临夏县| 湘潭市| 喀什市| 梨树县| 张家川| 江孜县| 霍城县| 霍州市| 图木舒克市| 岳西县| 军事| 井冈山市| 萝北县| 大荔县| 犍为县| 马尔康县| 茶陵县| 宜章县| 平武县| 驻马店市| 陈巴尔虎旗| 游戏| 科技| 汶上县| 竹北市| 普安县|