麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

詳解Spring Controller autowired Request變量

2024-09-05 00:23:19
字體:
來源:轉載
供稿:網友

詳解Spring Controller autowired Request變量

spring的DI大家比較熟悉了,對于依賴注入的實現也無須贅述。

那么spring的bean的默認scope為singleton,對于controller來說每次方法中均可以獲得request還是比較有意思的。

對于方法參數上的request通過構建方法的參數可以獲得最新的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等單實例變量來說如何動態注入變量呢?spring使用了很聰明的辦法

  1. 首先request和用戶請求相關
  2. 不同的用戶同時訪問時是在不同的線程中
  3. 保存了用戶的請求在threadlocal中
  4. 用戶獲取該請求需要手動調用threadlocal來獲取
  5. 為了幫助用戶減少重復代碼,spring可以讓用戶‘動態'注入request
  6. 當controller在實例化時,動態注冊一個proxy到當前request變量中
  7. 此proxy當被使用是可以將所有方法動態路由到threadlocal中該request變量上執行
/** * 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"; } } 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 草久在线观看视频 | 中文有码一区二区 | 激情大乳女做爰办公室韩国 | 黄网站在线免费看 | 26uuu成人人网图片 | 一级免费 | 亚洲国产精品一区 | 国产一区二区二 | 在线97视频| 日韩黄色一级视频 | 成人三级电影网址 | 亚洲天堂岛国片 | 国产精品成人亚洲一区二区 | 最新中文字幕在线视频 | 永久在线观看电影 | 色天天综合网 | 精品一区免费 | 91aa.app| 黄污免费网站 | 99亚洲伊人久久精品影院红桃 | 蜜桃一本色道久久综合亚洲精品冫 | 欧美另类69xxxxx 视频 | 久久精品男人 | 午夜精品成人一区二区 | 91网视频 | 国产一区二区影视 | 2018亚洲男人天堂 | 羞羞网站在线观看入口免费 | v11av在线视频成人 | 国产91影院| 欧美城网站地址 | 精国产品一区二区三区四季综 | 久久免费视频精品 | 日本不卡一区二区三区在线 | 九九热精品视频在线 | 叶子楣成人爽a毛片免费啪啪 | 午夜精品影院 | 国产精品免费在线 | 欧美精品在线视频观看 | 日日草夜夜 | 久久国产精品二国产精品中国洋人 |