記一次Springboot啟動異常

來源:互聯網
上載者:User

標籤:gap   tomcat   自己   mis   actor   missing   簡單   vax   min   

  啟動Springboot項目報以下異常:

  

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.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at hello.Application.main(Application.java:13) [classes/:na]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:204) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]    ... 8 common frames omitte

  很明顯是提示Application無法擷取ServletWebServerFactory執行個體,首先要注意, 由於Springboot在以往使用Spring都要配置下提供了整合方案,所以最簡單的方法就是採用預設的配置,解決方案非常簡單,只需在你的啟動類中加入@EnableAutoConfiguration,當然你也可以手動設定,如下為自動設定。

package hello;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import java.util.Arrays;@EnableAutoConfigurationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }    @Bean    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {        return args -> {            System.out.println("Let‘s inspect the beans provided by Spring Boot:");            String[] beanNames = ctx.getBeanDefinitionNames();            Arrays.sort(beanNames);            for (String beanName : beanNames) {                System.out.println(beanName);            }        };    }}

  手動設定請參考官網,地址:https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/reference/html/howto-embedded-web-servers.html#howto-configure-tomcat

  下面配一個全部手動設定的代碼,其中@value標籤的內容需要自己寫properties檔案:

package com.gws.configuration;import org.apache.catalina.connector.Connector;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.boot.web.servlet.MultipartConfigFactory;import org.springframework.boot.web.servlet.server.ServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.servlet.MultipartConfigElement;/** * 使用tomcat配置 * * @version  * @author  *  */@Configurationpublic class TomcatConfig {    @Value("${spring.server.port}")    private String port;    @Value("${spring.server.acceptorThreadCount}")    private String acceptorThreadCount;    @Value("${spring.server.minSpareThreads}")    private String minSpareThreads;    @Value("${spring.server.maxSpareThreads}")    private String maxSpareThreads;    @Value("${spring.server.maxThreads}")    private String maxThreads;    @Value("${spring.server.maxConnections}")    private String maxConnections;    @Value("${spring.server.protocol}")    private String protocol;    @Value("${spring.server.redirectPort}")    private String redirectPort;    @Value("${spring.server.compression}")    private String compression;    @Value("${spring.server.connectionTimeout}")    private String connectionTimeout;    @Value("${spring.server.MaxFileSize}")    private String MaxFileSize;    @Value("${spring.server.MaxRequestSize}")    private String MaxRequestSize;    @Bean    public ServletWebServerFactory servletContainer() {        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();        tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());        return tomcat;    }    @Bean    public MultipartConfigElement multipartConfigElement() {        MultipartConfigFactory factory = new MultipartConfigFactory();        //  單個資料大小        factory.setMaxFileSize(MaxFileSize); // KB,MB        /// 總上傳資料大小        factory.setMaxRequestSize(MaxRequestSize);        return factory.createMultipartConfig();    }    /**     *     * 預設http串連     *     * @version     * @author liuyi  2016年7月20日 下午7:59:41     *     */    public class GwsTomcatConnectionCustomizer implements TomcatConnectorCustomizer {        public GwsTomcatConnectionCustomizer() {        }        @Override        public void customize(Connector connector) {            connector.setPort(Integer.valueOf(port));            connector.setAttribute("connectionTimeout", connectionTimeout);            connector.setAttribute("acceptorThreadCount", acceptorThreadCount);            connector.setAttribute("minSpareThreads", minSpareThreads);            connector.setAttribute("maxSpareThreads", maxSpareThreads);            connector.setAttribute("maxThreads", maxThreads);            connector.setAttribute("maxConnections", maxConnections);            connector.setAttribute("protocol", protocol);            connector.setAttribute("redirectPort", "redirectPort");            connector.setAttribute("compression", "compression");        }    }}

 

記一次Springboot啟動異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.