Spring框架无法启动嵌入式容器
Spring框架无法启动嵌入式容器
我在跟随《Spring实战》第四版第五章的内容,但是我在第一个示例中卡住了。
这是我的Eclipse Luna项目结构:
如果我将这个项目作为Spring Boot应用程序运行,那么会抛出异常:
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:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %>Spittr " >Welcome to Spittr
">Spittles | ">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自己的依赖管理。使用提供的“starters”,可以确保所有需要的库都会以匹配的版本进行包含。
在pom.xml中,不要包含spring-mvc
的artifact:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency>
而是使用专门为web应用程序提供的Boot starter,即spring-boot-starter-web
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
更多细节请参考官方指南
我认为pom.xml的其他部分也是多余的(spring-boot-starter,javax.servlet-api,spring-boot-maven-plugin)
感谢您的解决方案,它不会再抛出此异常,但我仍然无法获取所需的页面,当在浏览器中输入localhost:8080时,出现错误页面,显示:"This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jun 20 14:56:10 PDT 2015 There was an unexpected error (type=Not Found, status=404). /WEB-INF/views/home.jsp" 这还是一个与Maven相关的问题吗?
将views文件夹移动到src/main/resources。存储模板的WEB-INF文件夹将不会被打包,并且在运行应用程序时不会在类路径中。