Spring Controller autowired Request variable, springautowired

Source: Internet
Author: User

Spring Controller autowired Request variable, springautowired

Spring Controller autowired Request variable

We are familiar with spring DI and do not need to repeat the implementation of dependency injection.

The default scope of spring bean is singleton. It is interesting for the controller to obtain the request in each method.

For requests on method parameters, the latest request can be obtained by constructing method parameters.

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. How to dynamically inject variables into single instance variables such as controller? Spring uses a clever method.

  1. First, the request is related to the user request.
  2. Different users access data in different threads at the same time.
  3. The user's request is saved in threadlocal.
  4. You need to manually call threadlocal to obtain this request.
  5. To help users Reduce repeated code, spring allows users to inject requests 'dynamically '.
  6. When the controller is instantiated, a proxy is dynamically registered to the current request variable.
  7. When used, this proxy can dynamically route all methods to threadlocal and execute this request variable.
/** * 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"; } } 

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.