1. 程式人生 > >Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

Spring Boot啟動出現錯誤,錯誤內容大概的意思是:未能載入嵌入的供web應用載入的空間,是因為缺少EmbeddedServletContainerFactory bean,在stackflow上面看到了這個錯誤的解決辦法,原文如下:

The scheduling guide isn't a web app so you probably have some mouldy stuff in your pom.xml from the REST guide? If you follow the instructions closely it should work. Another potential issue with the code you posted above is that your  @EnableAutoConfiguration class is not used in the context, only as a main method (which may not be a problem for the scheduling guide but it probably is for a bunch of others).


也就是說解決的辦法是在SpringBoot啟動類上加一個@EnableAutoConfiguration註解,但是畢竟是Spring Boot啊,只需要一個@SpringBootApplication註解即可搞定,因為@SpringBoootApplication註解已經帶了@EnableAutoConfiguration註解,並且還封裝了其他的註解,看看@SpringBootApplication的定義(註釋省略):

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

	Class<?>[] exclude() default {};

	String[] excludeName() default {};

	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};

	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};

}

我的問題是新增一個@SpringBootApplication註解來解決的:

package com.lanchangtou.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

當然是根據情況會出現這個問題,如果內嵌的tomcat版本不匹配也會有這個問題,也就是說在專案中又引入了一個Tomcat會出問題。

statckoverflow原文連結:http://stackoverflow.com/questions/21783391/spring-boot-unable-to-start-embeddedwebapplicationcontext-due-to-missing-embedd