详解Spring Controller autowired Request变量

发布时间:2022-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了详解Spring Controller autowired Request变量脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

详解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";
 }
 }
 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

脚本宝典总结

以上是脚本宝典为你收集整理的详解Spring Controller autowired Request变量全部内容,希望文章能够帮你解决详解Spring Controller autowired Request变量所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。