2014-12-14 5 views
1

web.xml 파일이없는 spring mvc 4 웹 응용 프로그램을 @Configuration 주석 만 사용하여 배포하려고합니다. 나는web.xml없이 spring mvc 4 응용 프로그램을 시작할 수 없습니다.

public class WebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) 
     throws ServletException { 
    WebApplicationContext context = getContext(); 
    servletContext.addListener(new ContextLoaderListener(context)); 
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
      "DispatcherServlet", new DispatcherServlet(context)); 
    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("*.html"); 
} 

private AnnotationConfigWebApplicationContext getContext() { 
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
    context.setConfigLocation("ge.dm.icmc.config.WebConfig"); 
    return context; 
} 

}

을 내 WebConfig.java 클래스는 다음과 같습니다

@Configuration  
@EnableWebMvc  
@ComponentScan(basePackages="ge.dm.icmc")  
public class WebConfig{ 

} 

하지만 응용 프로그램을 시작하려고 할 때, 나는 로그에 참조하십시오

(14) : 49 : 12.275 [localhost-startStop-1] DEBUG oswcsAnnotationConfigWebApplicationContext - 클래스 f를로드 할 수 없습니다. 또는 config location [] - 패키지 검사를 시도 중입니다. java.lang.ClassNotFoundException :

web.xml 파일을 추가하려고하면 정상적으로 시작됩니다.

+2

'setConfigLocation' 대신'register'를 사용하면 안됩니다. –

+0

Spring이 제공하는 편리한 클래스 중 하나를 사용하여'WebApplicationInitializer'를 구현하는 대신 제안 할 것입니다. –

+0

"봄이 제공하는 편의 수업"에서 무슨 뜻인지 말해 줄 수 있습니까? –

답변

7

이 경우에는 setConfigLocation 메서드를 사용하고 있습니다. 대신 register 방법을 사용해야합니다.

private AnnotationConfigWebApplicationContext getContext() { 
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
    context.register(ge.dm.icmc.config.WebConfig.class); 
    return context; 
} 

그러나 대신 WebApplicationInitializer을 구현 난 강력하게 이것에 대한 스프링의 편리한 클래스 중 하나를 사용하는 것이 좋습니다. 귀하의 경우에는 AbstractAnnotationConfigDispatcherServletInitializer이 도움이 될 것입니다.

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    protected Class<?>[] getRootConfigClasses() { return null;} 

    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { WebConfig.class}; 
    } 

    protected String[] getServletMappings() { 
     return new String[] {"*.html"}; 
    } 
}