Spring框架无法启动嵌入式容器

我正在关注Spring in Action第4版第5章,但我仍然坚持第一个例子。

这是我的Eclipse Luna项目结构:

项目结构

如果我将此项目作为Spring Boot App运行,那么它会抛出exception:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at spittr.SpittrApplication.main(SpittrApplication.java:10) Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ... 7 common frames omitted 

我该如何解决这个问题?

所有文件的内容:

SpittrApplication.java:

 package spittr; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpittrApplication { public static void main(String[] args) { SpringApplication.run(SpittrApplication.class, args); } } 

SpittrWebAppInitializer.java:

 package spittr.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected Class[] getRootConfigClasses() { return new Class[] { RootConfig.class }; } @Override protected Class[] getServletConfigClasses() { return new Class[] { WebConfig.class }; } } 

WebConfig.java:

 package spittr.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan("spittr.web") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } } 

RootConfig.java:

 package spittr.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages = { "spitter" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) }) public class RootConfig { } 

HomeController.java:

 package spittr.web; import static org.springframework.web.bind.annotation.RequestMethod.*; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value = "/", method = GET) public String home() { return "home"; } } 

针对home.jsp:

     Spittr <link rel="stylesheet" type="text/css" href="https://stackoverflow.com/questions/30958968/spring-framework-unable-to-start-embedded-container/" >   

Welcome to Spittr

<a href="https://stackoverflow.com/questions/30958968/spring-framework-unable-to-start-embedded-container/">Spittles | <a href="https://stackoverflow.com/questions/30958968/spring-framework-unable-to-start-embedded-container/">Register

的pom.xml

   4.0.0 com.spittr spittr 0.0.1-SNAPSHOT jar spittr Demo project for Spring Boot  org.springframework.boot spring-boot-starter-parent 1.2.4.RELEASE     UTF-8 1.8    org.springframework.boot spring-boot-starter   org.springframework spring-webmvc   javax.servlet javax.servlet-api provided   org.springframework.boot spring-boot-starter-test test      org.springframework.boot spring-boot-maven-plugin     

使用Spring Boot时,不应直接包含其他Spring依赖项,而应依赖Boot自己的依赖项管理。 使用提供的“启动器”时,您可以确保所有需要的库都包含在匹配的版本中。

而不是在你的pom.xml中包含spring-mvc工件:

  org.springframework spring-webmvc  

使用专用的Boot starter for webapps, spring-boot-starter-web

  org.springframework.boot spring-boot-starter-web  

官方指南中的更多细节


我相信你的pom.xml的其他部分也是多余的(spring-boot-starter,javax.servlet-api,spring-boot-maven-plugin)

如果您正在使用另一个侦听与Tomcat侦听相同的端口的应用程序,则这也可能是问题所在。