欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring Controller autowired Request变量

程序员文章站 2023-11-07 19:13:46
详解spring controller autowired request变量 spring的di大家比较熟悉了,对于依赖注入的实现也无须赘述。 那么spring的be...

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

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