簡體   English   中英

Spring Boot:由於缺少 EmbeddedServletContainerFactory bean 無法啟動 EmbeddedWebApplicationContext

[英]Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

我對 Spring 完全陌生,並開始從這個網站做官方指南: https : //spring.io/guides

我想做這個指南: https : //spring.io/guides/gs/scheduling-tasks/

我收到以下異常:

2014-02-14 16:25:21.614  INFO 9032 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springframework.scheduling.annotation.SchedulingConfiguration$$EnhancerByCGLIB$$5b48d763] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-02-14 16:25:21.638  INFO 9032 --- [           main] .c.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/work/Spring/SpringTutorial/target/classes/, file:/C:/work/apache-maven-3.0.3/repo/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter/1.0.0.RC1/spring-boot-starter-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot/1.0.0.RC1/spring-boot-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-autoconfigure/1.0.0.RC1/spring-boot-autoconfigure-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-logging/1.0.0.RC1/spring-boot-starter-logging-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jcl-over-slf4j/1.7.5/jcl-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jul-to-slf4j/1.7.5/jul-to-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/log4j-over-slf4j/1.7.5/log4j-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-core/1.0.13/logback-core-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-web/1.0.0.RC1/spring-boot-starter-web-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-tomcat/1.0.0.RC1/spring-boot-starter-tomcat-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-logging-juli/7.0.47/tomcat-embed-logging-juli-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-webmvc/4.0.0.RELEASE/spring-webmvc-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-databind/2.3.1/jackson-databind-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-annotations/2.3.0/jackson-annotations-2.3.0.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-core/2.3.1/jackson-core-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/commons-lang/commons-lang/2.2/commons-lang-2.2.jar]
Exception in thread "main" 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:140)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:920)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
    at hu.kumite.Application.main(Application.java:17)
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:190)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:163)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
    ... 7 more

應用程序入門類是這樣的:

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

如您所見,main 方法包含一個注釋行。 我已經完成了一個教程,即這個: https : //spring.io/guides/gs/sumption-rest/它已啟動並運行。 但我無法運行 ScheduledTasks 應用程序,如下所示:

@EnableScheduling
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("The time is now " + dateFormat.format(new Date()));
    }
}

我使用 Eclipse 並將 Application.java 的 main 作為應用程序運行。 有人可以幫我嗎?

調度指南不是一個網絡應用程序,所以你的 pom.xml 中可能有一些來自 REST 指南的發霉的東西? 如果您嚴格按照說明操作,它應該可以工作。 您上面發布的代碼的另一個潛在問題是您的@EnableAutoConfiguration類未在上下文中使用,僅用作主要方法(這可能不是調度指南的問題,但可能是其他一些問題)。

@SpringBootApplication的掃描顯示它包含以下注釋:

@Configuration
@ComponentScan
@EnableAutoConfiguration

所以你也可以這樣做:

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

在你的 pom.xml 中使用這個:

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

或者這個:

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

我在一個包含 Web 啟動的 Spring Boot 項目中有多個應用程序類,並希望避免為其中之一配置 Web 環境,因此我手動將其配置如下:

@SpringBootApplication
public class Application
{
    public static void main(String[] args)
    {
        new SpringApplicationBuilder(Application.class)
            .web(false)
            .run(args);
    }
}

UPDATE春季啟動2及以上:

@SpringBootApplication
public class Application
{
    public static void main(String[] args)
    {
        new SpringApplicationBuilder(Application.class)
            .web(WebApplicationType.NONE)
            .run(args);
    }
}

嘗試這個

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

該錯誤表明您嘗試運行的應用程序無法實例化 apache tomcat 的實例。 確保您正在使用 tomcat 運行應用程序。

如果在檢查完所有依賴項后遇到同樣的問題,請嘗試在配置類中添加以下內容

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = 
                  new TomcatEmbeddedServletContainerFactory();
    return factory;
 }

如果您使用的是 tomcat 的外部實例(特別是對於 intellij),則問題可能是 IDE 正在嘗試啟動嵌入式 tomcat。 在這種情況下,從 pom.xml 中刪除以下內容,然后使用“編輯配置”向導配置外部 tomcat。

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
</dependency> 

添加

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

如果您在使用 intellij 時遇到此異常,並且您正在嘗試使用run按鈕啟動應用程序。 嘗試從命令行啟動應用程序。 例如,假設這是一個springboot應用程序,請確保您位於正確的目錄中(包含 pom 文件的目錄) run mvn spring-boot:run這對我有用。

此外,我還看到當您的 spring 應用程序依賴於另一個應用程序時會發生此錯誤。 在這種情況下,我必須先啟動另一個應用程序,然后再運行。

添加注解@SpringBootApplication在入門類為我解決這個問題之前(所以本質上,這個錯誤信息可能意味着“你沒有一個@SpringBootApplication標記的類,你至少需要一個)

@SpringBootApplication
public class AppStarter {

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

當 main 方法位於與傳遞給 SpringApplcation.run() 的類不同的類上時,我遇到了類似的問題

因此,解決方案是使用您注釋掉的行:

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

如果您將其打包為單個 jar 並且它不是 Web 應用程序,請嘗試加載應用程序上下文,如下所示。

@SpringBootApplication

ApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class);

或使用以下插件打包為單個 jar

             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

您可以使用以下命令指定外部配置來運行

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

/ http://docs.spring.io/spring-boot/docs/current/reference/htmlboot-features-external-config.html#boot-features-external-config-application-property-files

請注意,如果您將屬性作為參數傳遞,則不要包含@PropertySource("classpath:test.properties")它將覆蓋參數

如果您使用命令行gradle bootRun成功運行它,同時使用命令行gradle jargradle jar到 jar 文件以便使用命令行java -jar build/libs/demo.jar運行它,不幸的是,它失敗並出現異常: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean ,在這種情況下,您需要使用 gradle 插件spring-boot任務bootRepackage來生成特殊的可運行 jar。

  • 設置 1

    $ gradle clean boot重新打包

  • 設置 2

    $ java -jar build/libs/demo.jar

SpringApplication將嘗試代表您創建正確類型的ApplicationContext 默認情況下,將使用AnnotationConfigApplicationContextAnnotationConfigEmbeddedWebApplicationContext ,具體取決於您是否正在開發 Web 應用程序。

用於確定“網絡環境”的算法相當簡單(基於幾個類的存在)。 如果需要覆蓋默認setWebEnvironment(boolean webEnvironment)可以使用setWebEnvironment(boolean webEnvironment)

也可以通過調用setApplicationContextClass(…​)來完全控制將使用的ApplicationContext類型。

[提示] 在 JUnit 測試中使用SpringApplication時,通常需要調用setWebEnvironment(false)

添加 spring boot starter 依賴項修復了我的錯誤。

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

如果您想將 tomcat 作為嵌入式服務器啟動,這是必需的。

檢查您的 pom.xml 是否存在

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

我遇到過這樣的問題;因為缺少這種依賴

在我的例子中,我們新添加了 @Profile 注釋,以便忽略生產模式下的 TestApplication 類和測試模式下的 Application 類。

不幸的是,我們忘記將以下行添加到 application.properties 文件中:

spring.profiles.active=test
or
spring.profiles.active=production

如果沒有這些配置,就不會加載配置文件,這會導致不太常見的 Spring 錯誤。

這應該是依賴問題導致的,一般情況下需要檢查依賴。

這個類的問題是:

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

啟動應用程序的正確方法是:

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

清除存儲庫是一種可能的解決方案。

Windows -> 刪除 maven 存儲庫中的所有子文件夾:

C:\\Users\\YourUserName.m2\\repository

我遇到了同樣的問題。 因為我沒有使用 Maven 在 Spring-Boot 中定義 Main.class 和以下注釋:

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

可能您在 Spring Boot 入門課程中缺少@SpringBootApplication

@SpringBootApplication
public class LoginSecurityAppApplication {

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

}

問題是排除starter tomcat,我嘗試排除它並使用vert.x,所以當我與Spring Admin集成時,開始出現問題

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

我在以下情況下有此異常。

在我的 POM 中是屬性:

<properties>
    <java.version>1.8</java.version>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.scmaer.java.microservice.Application</start-class>
    <cxf.version>3.1.5</cxf.version>
    <olingo.version>2.0.10</olingo.version>
    <spring.boot.version>1.4.7.RELEASE</spring.boot.version>
    <spring.boot.plugin.version>1.5.8.RELEASE</spring.boot.plugin.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <skipTests>false</skipTests>
</properties>

並且我的應用程序類(“start-class”)的名稱和路徑是錯誤的。

我有一個類似的問題,問題是一個損壞的 maven repo jar 文件。 就我而言, tomcat-embed-core jar 文件已損壞。 所以我從 maven repo 中刪除了它並刷新它以再次下載。

在我的情況下,它發生在使用以下代碼從pom排除資源文件夾之后。

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*/*.properties</exclude>
            </excludes>
        </resource>
</resources>

注釋此代碼開始了我的代碼。

此問題的另一個原因是 maven 存儲庫 jar 損壞,因此您可以使用以下命令來解決問題:

mvn dependency:purge-local-repository

我正在使用 gradle,當我有一個 commandLineRunner 使用 kafka 主題和一個用於接收傳入鈎子的健康檢查端點時遇到了問題。 花了12個小時摸索,終於發現我用的是mybatis-spring-boot-starter和spring-boot-starter-web,它們有一些沖突。 后來我直接引入了mybatis-spring、mybatis和spring-jdbc而不是mybatis-spring-boot-starter,程序運行良好。

希望這可以幫助

就我而言,彈簧配置未按預期加載。 使用以下命令從 cmd 運行時,它起作用了:

start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM