스프링 컨트롤 러 autowired 요청 변 수 를 자세히 설명 합 니 다.

3891 단어 SpringController
스프링 컨트롤 러 autowired 요청 변 수 를 자세히 설명 합 니 다.
spring 의 DI 는 여러분 에 게 익숙 하 므 로 주입 에 의존 하 는 실현 에 대해 서도 긴 말 할 필요 가 없습니다.
그러면 spring 의 bean 의 기본 scope 는 singleton 으로 contrller 에 있어 서 매번 방법 에서 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 은 똑똑 한 방법 을 사 용 했 습 니 다.
우선 request 와 사용자 요청 관련
  • 서로 다른 사용자 가 동시에 방문 할 때 서로 다른 스 레 드 에서
  • 사용자 의 요청 을 threadlocal 에 저 장 했 습 니 다
  • 사용자 가 이 요청 을 받 으 려 면 threadlocal 을 수 동 으로 호출 하여 가 져 와 야 합 니 다
  • 4.567917.사용자 가 중복 코드 를 줄 일 수 있 도록 spring 은 사용자 에 게'동적'으로 request 를 주입 할 수 있 습 니 다
  • contrller 가 실례 화 될 때 현재 request 변수 에 proxy 를 동적 으로 등록 합 니 다
  • 이 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";
     }
     }
     
    
    궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

    좋은 웹페이지 즐겨찾기