2014-04-28 3 views
2

내가 톰캣 8 용기 내장 된 WebApplicationInitializer 사용하여 봄의 WebApplicationContext를 만들려면 및이의 WebApplicationContext에 대한 부모 컨텍스트을 제공 로합니다.프로그래밍 방식으로 생성 된 상위 컨텍스트를 사용하여 포함 된 Servlet 3 환경에서 Spring (4) XML 기반 WebApplicationContext를 만드는 방법은 무엇입니까?

어떻게 내가 내 코드에서 할 것은 :

@Override 
public void onStartup(final ServletContext servletContext) throws ServletException 
{ 
    SpringContext parentContext = ... obtain parent, i know how ... 
    WebAppContext webCtx = new WebAppContext("classpath:sample/web.xml", 
     parentContext); // how can i do this? 

    // Manage the lifecycle of the root application context 
    servletContext.addListener(new ContextLoaderListener(webCtx)); // correct? 


    // now create dispatcher servlet using WebAppContext as parent 
    DispatcherServlet servlet = ... perform creation ... 
    // how? 
} 
:
Tomcat t = new Tomcat() 
// ... some configuration ... 
t.start(); 

그래서 내가 WebApplicationInitializer의 구현을 찾고 있어요 :

ApplicationContext context = new ClassPathXmlApplicationContext(new 
    String[{"samples/context.xml"}); 
// ... here i do funny things with the context ... 

내가 톰캣 8 인스턴스를 생성보다

i 원하지 않는

(새로운 상황에 대한 부모로 미리 빌드 제공 컨텍스트를 사용하도록 로더에게 어떻게 재미있을 것, 심지어 thoug) 톰캣 시작시 WebAppContext을 만들 수있는 web.xml에게 고전 ContextLoaderListener에를 사용하는

<import resource="classpath*:/META-INF/whatever/root/to/otherAppContext.xml" /> 

도 내가 AnnotationConfigWebApplicationContext를 사용 주석 중심의 접근 방식을 사용하지 않으 : 나는 또한 사용하고 싶지 않아요.

을 사용하고 싶지 않습니다. XML 정의를 가져 오기 위해 WebAppContext에서 가져 오기 - 속임수를 사용하고 싶지 않습니다. 사용

기술 : 봄 4.0.3, 톰캣 (8), 자바 8SE

어떤 제안 어떻게 내 WebApplicationInitializer의 onStartup (...) 방법을 구현하는 방법? 나는 Spring explanation을 보았다. 나를 도와주지 않았다. 에 구체적인 작업 코드을 입력하십시오.

들으,

답변

1

이 나를 위해 일한 :

@Override 
public void onStartup(final ServletContext servletContext) throws ServletException 
{ 
    final ApplicationContext parent = new ClassPathXmlApplicationContext(new String[ 
    {"/com/mydomain/root.context.xml"}); 

    XmlWebApplicationContext context = new XmlWebApplicationContext(); 
    context.setConfigLocation("classpath:/com/mydomain/childContext.xml"); 
    context.setParent(parent); 

    ConfigurableWebApplicationContext webappContext = (ConfigurableWebApplicationContext) 
    context; 
    webappContext.setServletContext(servletContext); 
    webappContext.refresh(); 


    servletContext.addListener(new ContextLoaderListener(webappContext)); 

    // ... register dispatcher servlets here ... 
} 

HTH,