Spring Boot 错误:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext

Posted

技术标签:

【中文标题】Spring Boot 错误:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext【英文标题】:Spring Boot error: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 【发布时间】:2016-03-05 12:06:06 【问题描述】:

我正在尝试使用 Spring Data GemFire 将数据放入 GemFire。

我关注了这个link

@Region("stockdata")
public class StockInfo 

    @Id 
    public String symbol;

    public String price;

    @PersistenceConstructor
    public StockInfo(String symbol, String price) 
        super();
        this.symbol = symbol;
        this.price = price;
    

    @Override
    public String toString() 
        return "StockInfo [symbol=" + symbol + ", price=" + price + "]";
    

    public String getSymbol() 
        return symbol;
    
    public void setSymbol(String symbol) 
        this.symbol = symbol;
    
    public String getPrice() 
        return price;
    
    public void setPrice(String price) 
        this.price = price;
    


StockRepository 类:

public interface StockRepository extends CrudRepository<StockInfo, String> 
    StockInfo findByName(String symbol);


主类:

@Configuration
@EnableGemfireRepositories
public class Application implements CommandLineRunner 

    @Bean
    CacheFactoryBean cacheFactoryBean() 
        return new CacheFactoryBean();

    

    @Bean
    LocalRegionFactoryBean<String, StockInfo> localRegionFactory(final GemFireCache cache) 
        return new LocalRegionFactoryBean<String, StockInfo>() 

            
                setCache(cache);
                setName("stockdata");
                setClose(false);
            
        ;
    
    @Autowired
    StockRepository stockRepositry;
    public void run(String... arg0) throws Exception 
        StockInfo fbStock = new StockInfo("FB", "100");
        StockInfo aaplStock = new StockInfo("AAPL", "200");
        StockInfo msftStock = new StockInfo("MSFT", "300");

        System.out.println("Before linking up with Gemfire...");
        for (StockInfo stockInfo : new StockInfo[] fbStock, aaplStock,msftStock ) 
            System.out.println("\t" + stockInfo);
        

        stockRepositry.save(fbStock);
        stockRepositry.save(aaplStock);
        stockRepositry.save(msftStock);

        System.out.println("Lookup each Stock by name...");

        for (String symbol : new String[]  fbStock.symbol, aaplStock.symbol,msftStock.symbol ) 
            System.out.println("\t" + stockRepositry.findByName(symbol));
        

    

    public static void main(String[] args) throws IOException 
        SpringApplication.run(Application.class, args);
    

Pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.7</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-gemfire</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency> 
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories> 

错误如下:

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) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.emc.geode.entity.Application.main(Application.java:62) [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:183) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 8 common frames omitted

【问题讨论】:

Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean的可能重复 你的 pom 中很可能缺少 spring-boot-maven-plugin @jny .它在那里.. org.springframework.bootspring-boot-maven-plugin 如何构建和运行它? java -jar GeodeRest-0.0.1-SNAPSHOT.jar 【参考方案1】:

您需要将@SpringBootApplication 添加到您的主类中。

@EnableGemfireRepositories
@SpringBootApplication
public class Application implements CommandLineRunner 

在你的 pom 中添加 spring-boot-starter-web 依赖而不是 spring-web

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

【讨论】:

【参考方案2】:

为了在您的 Spring Boot 应用程序中简化您的 Pivotal GemFire 配置,您可以考虑使用(开始)Spring Boot for Pivotal GemFire (SBDG)project.

SBDG 建立在 Spring Data for Pivotal GemFire (SDG) 以及其他 Spring 项目(显然是 Spring Boot 以及 Pivotal GemFire 春季会议 (SSDG;here) 也是如此。它在开发 Pivotal GemFire 应用程序时应用了 Spring Boot 的所有概念(例如,使用自动配置的“约定优于配置”等)一般使用 Spring,尤其是 Spring Boot

例如,在您的应用程序中,SBDG 会自动自动配置 SD[G] 存储库,从而无需显式声明 @EnableGemfireRepositories

使用 SBDG 还有许多其他好处。

值得深思。

【讨论】:

实际上,我注意到您使用的是 Spring Boot 1.x,当时它有 spring-boot-starter-data-gemfire 模块。自 Spring Boot 2.0 起,此启动模块不再存在,并被我在上面的答案中引用的 SBDG 项目所取代。

以上是关于Spring Boot 错误:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext的主要内容,如果未能解决你的问题,请参考以下文章

由于缺少 WebApp 库,在 Spring-boot-starter 项目中构建失败

Spring Boot Test 失败说,由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext

Spring Boot Test 失败说,由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext

Spring Boot:当 spring-boot-starter-tomcat 和 tomcat-embed-jasper 一起时“扫描失败”

由于缺少 ServletWebServerFactory bean,Spring Boot jar 无法启动 Web 服务器

Spring Boot / Tomcat,“由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext”