Spring MVC +嵌入式Tomcat不起作用

时间:2017-05-22 20:45:08

标签: java spring spring-mvc tomcat embedded-tomcat-7

我是Java Spring的新手。我想通过以下方式将Tomcat配置为嵌入式servlet: 的 EmbeddeApp.java

public class EmbeddedApp {
    public static void main(String[] args) throws Exception {   

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        EmbeddedAppWebInitializer       
        webContext.register(EmbeddedAppWebInitializer.class);

        Tomcat tomcat = new Tomcat();

        File base = new File("WebContent/WEB-INF/views/");
        System.out.println(base.getAbsolutePath());
        Context rootCtx = tomcat.addContext("/", base.getAbsolutePath());            
        Tomcat.addServlet(rootCtx, "embeddedAppDispatcher", new DispatcherServlet(webContext));
        rootCtx.addServletMapping("/*", "embeddedAppDispatcher");

       tomcat.setPort(8080);
       tomcat.start();
       tomcat.getServer().await();

    }   
}

EmbeddedAppWebInitializer.java

public class EmbeddedAppWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer  {

    @Override
    protected String[] getServletMappings() {
        System.out.println("TEST @@@@ ------------------- ");
        return new String[] { "/*" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

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

    @Override
    protected String getServletName() {
        return  "embeddedAppDispatcher";
    }

}

文件结构:

File Structure Picture

启动后,EmbeddedAppWebInitializer未初始化(没有跟踪&#34; TEST @@@@ -------------------&#34;在Eclipse控制台中)以及之后在网络浏览器中输入: http://localhost:8080/EmbeddedApplication/weather 出现以下错误: - 在网络浏览器中:

javax.servlet.ServletException: Error allocating a servlet instance
    Eclipse中的
INFO: Starting service Tomcat
    maj 22, 2017 10:28:10 PM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.56
    maj 22, 2017 10:28:10 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    maj 22, 2017 10:28:15 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'embeddedAppDispatcher'
    maj 22, 2017 10:28:17 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Allocate exception for servlet embeddedAppDispatcher
    java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.
        at org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.setServletContext(DefaultServletHttpRequestHandler.java:106)
        at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:75)
        at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:64)
        at webService.config.WebConfig.configureDefaultServletHandling(WebConfig.java:32)
        at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.configureDefaultServletHandling(WebMvcConfigurerComposite.java:73)
        at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.configureDefaultServletHandling(DelegatingWebMvcConfiguration.java:72)
        at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:498)
        at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$c6b2fbdc.CGLIB$defaultServletHandlerMapping$40()
        at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$c6b2fbdc$$FastClassBySpringCGLIB$$a53c55e6.invoke()
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
        at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$c6b2fbdc.defaultServletHandlerMapping()
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.startup.Tomcat$ExistingStandardWrapper.loadServlet(Tomcat.java:866)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)

有谁知道如何解决问题,或者导致这个问题的原因是什么?

通过&#39; Run As&#39;启动应用程序时,一切看起来都不错 - &GT; &#39;在服务器上运行&#39;:

Servlet is initialized - trace: "TEST @@@@ ------------------- " is visible in console during startup. 

0 个答案:

没有答案