SpringBoot 報錯 start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

前兩天的項目,沒上傳到git上,今天又繼續碼代碼,出現了奇怪的問題。

兩天代碼沒動怎麼會出現這樣的問題,很是鬱悶。。。。。。。。。。。。。。。。。。

問題:啓動項目的時候

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at com.dataGovernance.DataGovernanceApplication.main(DataGovernanceApplication.java:14) [classes/:?]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	... 8 more;

上面的問題很明顯是:due to missing ServletWebServerFactory bean
但是爲啥會出現這樣的問題?????
代碼如下:

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DataGovernanceApplication.class);
    }
}

pom.xml

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

註解@SpringBootApplication,注了;
繼承SpringBootServletInitializer 類,重寫了configure方法;
pom文件的依賴也導入了;

也從百度上查找了好多,基本上這個問題就是上面說的這三個問題
也看了SpringBootServletInitializer源碼,模模糊糊。

最後嘗試的方法,換了啓動類的類名,啓動方法的方法名,奇怪的是竟讓啓動成功,沒在報錯。。。。。。
變成

public class DataGovernanaceApplication extends SpringBootServletInitializer

又嘗試了幾遍,直接修改類名還不行,先修改方法名,再去修改類型,纔可以。

由於項目比較着急,只能先這樣碼代碼了,後面再解決吧

在此,也請各位大佬們,如果知道是什麼問題,給講解一下,非常感謝!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章