2011-12-02 4 views
3

일반적으로 ApplicationContext (부모)과 0..n DispatcherServlets (자식)이 있습니다. DispatcherServlet이있는 것이 가능합니다. DispatcherServlet가 부모 컨텍스트으로 부모가 ApplicationContext 인 경우? 내가 알기에, bean은 일시적으로 해결 될 수 있으므로 애플리케이션 컨텍스트에 액세스 할 수 있어야합니다.spring DispatcherServlet 컨텍스트 상속

DispatcherServlet에 노출되어서는 안되기 때문에 공유 빈을 ApplicationContext에 넣고 싶지 않습니다. 한 가지 예외는 있습니다.

<servlet> 
    <servlet-name>foo</servlet-name> 
    <servlet-class>...DispatcherServlet</servlet-class> 
</servlet> 

<servlet> 
    <servlet-name>bar</servlet-name> 
    <servlet-class>...DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextAttribute</param-name> 
     <param-value>foo-servlet</param-value> 
    </init-param> 
</servlet> 
+0

왜 호기심이 궁금하십니까? – skaffman

+0

나는 전통적인 저장소 - 서비스 - 컨트롤러 아키텍처를 가지고있다. 기존 컨트롤러 레이어 위에 다른 레이어를 추가하고 싶습니다. – Jan

답변

1

내가 DispatcherServlet을 확장 : 당신은 그 자체로 foobar 사용 환경을 만들기 위해 다음과 같은 작업을 수행 할 수처럼

1

HttpServletBeanFrameworkServlet에서 그것은 보인다. 이제 완벽하게 작동합니다!

public class ConfigurableDispatcherServlet extends DispatcherServlet { 

    private String contextParent; 

    /** 
    * Initialize and publish the WebApplicationContext for this servlet. 
    * <p> 
    * Delegates to {@link #createWebApplicationContext} for actual creation of 
    * the context. Can be overridden in subclasses. 
    * 
    * @return the WebApplicationContext instance 
    * @see #setContextClass 
    * @see #setContextConfigLocation 
    */ 
    protected WebApplicationContext initWebApplicationContext() { 
     // No fixed context defined for this servlet - create a local one. 
     WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext(), 
       "org.springframework.web.servlet.FrameworkServlet.CONTEXT." + getContextParent()); 
     WebApplicationContext wac = createWebApplicationContext(parent); 

     // Publish the context as a servlet context attribute. 
     String attrName = getServletContextAttributeName(); 
     getServletContext().setAttribute(attrName, wac); 
     if (this.logger.isDebugEnabled()) { 
      this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() + 
         "' as ServletContext attribute with name [" + attrName + "]"); 
     } 
     if(this.logger.isInfoEnabled()) { 
      this.logger.info(getServletName() + " is a child of " + parent.getDisplayName()); 
     } 

     return wac; 
    } 

    public String getContextParent() { 
     return contextParent; 
    } 

    public void setContextParent(String contextParent) { 
     this.contextParent = contextParent; 
    } 
} 
+0

좋아 보이는데, 아프다. – Jan

+0

슬프게도 실패합니다 : "No WebApplicationContext found : initializer not registered?" 너에게 효과가 있니? – Jan

+0

"No WebApplicationContext found : initializer not registered?"로 실패했습니다. 하지만 contextAttribute 앞에 "org.springframework.web.servlet.FrameworkServlet.CONTEXT"를 추가해야한다는 것을 알게되었습니다. 이제는 올바르게로드되지만 "contextConfigLocation"은 알 수 없습니다 (또는 다른 이유로 인해 컨텍스트 xml 파일이로드되지 않아 컨트롤러가 시작되지 않습니다). 어떤 생각? – Jan

관련 문제