2017-04-17 156 views
2

我收到類似下面的錯誤,當我開始springboot應用SpringBoot - 無法啓動嵌入式容器

. ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.2.RELEASE) 

2017-04-17 13:28:59.575 INFO 986 --- [   main] c.f.s.c.SpringBootLoginController  : Starting SpringBootLoginController on SoftAuthorThrees-Mac-mini.local with PID 986 (/Users/softauthorthree/git/HelloSpringBootRepo/HelloSpringBoot/target/classes started by softauthorthree in /Users/softauthorthree/git/HelloSpringBootRepo/HelloSpringBoot) 
2017-04-17 13:28:59.575 INFO 986 --- [   main] c.f.s.c.SpringBootLoginController  : No active profile set, falling back to default profiles: default 
2017-04-17 13:28:59.575 DEBUG 986 --- [   main] o.s.boot.SpringApplication    : Loading source class com.test.springboot.controller.SpringBootLoginController 
2017-04-17 13:28:59.605 DEBUG 986 --- [   main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/Users/softauthorthree/git/HelloSpringBootRepo/HelloSpringBoot/target/classes/application.properties' (classpath:/application.properties) 
2017-04-17 13:28:59.606 DEBUG 986 --- [   main] o.s.b.c.c.ConfigFileApplicationListener : Skipped (empty) config file 'file:/Users/softauthorthree/git/HelloSpringBootRepo/HelloSpringBoot/target/classes/application.properties' (classpath:/application.properties) for profile default 
2017-04-17 13:28:59.609 INFO 986 --- [   main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]70a9f84e: startup date [Mon Apr 17 13:28:59 IST 2017]; root of context hierarchy 
2017-04-17 13:28:59.611 DEBUG 986 --- [   main] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot[email protected]70a9f84e: org.s[email protected]12b0404f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,springBootLoginController]; root of factory hierarchy 
2017-04-17 13:28:59.701 DEBUG 986 --- [   main] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [[email protected]c065] 
2017-04-17 13:28:59.702 DEBUG 986 --- [   main] ationConfigEmbeddedWebApplicationContext : Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationE[email protected]] 
2017-04-17 13:28:59.705 WARN 986 --- [   main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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. 
2017-04-17 13:28:59.720 ERROR 986 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

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:137) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at com.test.springboot.controller.SpringBootLoginController.main(SpringBootLoginController.java:24) [classes/:na] 
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:189) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
... 8 common frames omitted 

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.test.springboot</groupId> 
    <artifactId>HelloSpringBoot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>HelloSpringBoot</name> 
    <description>HelloSpringBoot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.2.RELEASE</version> 
    </parent> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
    </dependencies> 
</project> 

我的控制器:

import org.springframework.boot.*; 
import org.springframework.web.bind.annotation.*; 

@RestController 
public class SpringBootLoginController { 

    @RequestMapping("/hello") 
    String hello() { 
     return "Hello World!!!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SpringBootLoginController.class, args); 
    } 

} 

回答

2

通過增加@SpringBootApplication解決此問題。

@SpringBootApplication 
@RestController 
public class SpringBootLoginController { 

    @RequestMapping("/hello") 
    String hello() { 
     return "Hello World!!!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SpringBootLoginController.class, args); 
    } 
} 

另外,通過添加@EnableAutoConfiguration也可以解決此問題。

@EnableAutoConfiguration 
@RestController 
public class SpringBootLoginController { 

    @RequestMapping("/hello") 
    String hello() { 
     return "Hello World!!!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SpringBootLoginController.class, args); 
    } 
} 
6

嘗試使用@SpringBootApplication批註註釋SpringBootLoginController類。

@SpringBootApplication 
@RestController 
public class SpringBootLoginController { 

    @RequestMapping("/hello") 
    String hello() { 
     return "Hello World!!!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SpringBootLoginController.class, args); 
    } 
} 
-1

您的依賴關係存在問題。收藏此

<dependency> 
<groupId><groupId></groupId> 
<artifactId><some dependency></artifactId> 
<version><version></version> 
<exclusions> 
    <exclusion> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
    </exclusion> 
</exclusions> 

0
在我的情況下加

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>2.5</version> 
</dependency> 

解決問題