启动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;
@EnableAutoConfiguration
public 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
*
*/
@Configuration
public 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启动异常的更多相关文章

  1. SpringBoot启动异常 Process finished with exit code 1

    记录一下一个报错 : < Springboot项目启动之后直接 Process finished with exit code 1 1. 是否有spring-boot-starter-web依赖 ...

  2. 解决spring-boot启动异常Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

    第一种: 需要在主类头加上  @EnableAutoConfiguration 第二种: pom文件是否加了 <dependency> <groupId>org.mybatis ...

  3. springboot启动异常:java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx.xxx.xxx' in value "${xxx.xxx.xxx}"

    场景: 本地启动正常,部署到服务器上启动时启动tomcat失败,显示上面的问题. 原因: 本地打包的时候没有修改指定的配置文件名称(本地只有一份配置文件). 在打包到服务器上时指定的配置文件命名会去查 ...

  4. springboot启动异常java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS

    解决办法一 yml或者Properties文件中配置 spring.freemarker.check-template-location=false 解决办法二 @SpringBootApplicat ...

  5. SpringBoot | 启动异常 | 显示bulid success 无 error信息

    可能原因是没有添加 web 依赖,检查pom里面是否有web <dependency> <groupId>org.springframework.boot</groupI ...

  6. 解决springboot启动日志异常问题

    问题描述:springboot启动异常,启动后没有日志打印. 问题原因:slf4j日志实现重复,找不到对应实现类. 问题应对: 1. 是不是项目没起来---->打印的日志数据,到这里就不打印了, ...

  7. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

  8. 关于 IDEA 启动 springboot 项目异常 - Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'

    关于 IDEA 启动 springboot 项目异常 - Disconnected from the target VM, address: '127.0.0.1:59770', transport: ...

  9. SpringBoot启动流程源码分析

    前言 SpringBoot项目的启动流程是很多面试官面试中高级Java程序员喜欢问的问题.这个问题的答案涉及到了SpringBoot工程中的源码,也许我们之前看过别的大牛写过的有关SpringBoot ...

随机推荐

  1. Node.js文件编码格式的转换

    项目很多 lua 文件不是 utf-8格式,使用 EditPlus 查看的时候,显示为ASCII.还有的是带BOM的,带BOM倒好处理,之前写过,有一定规律. ASCII编码就比较蛋疼,通过搜索网上资 ...

  2. GBK转UTF8

    shell 脚本自动GBK转UTF8 for i in `find . -name "*.java"`; do iconv -f gbk -t utf-8 $i > $i.n ...

  3. [CSS] Useful CSS tool for Web designer and developer

    1. Color Picker (Chrome) You might know how to use color picker in Chrome, recently there is a featu ...

  4. pip install whl

    $ pip install --download /tmp/offline_packages -r requirements.txt $ pip install --no-index --find-l ...

  5. java AOP Before, After, AfterReturning, AfterThrowing, or Around 注解

    https://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html Advice In this sectio ...

  6. AngularJS 使用ng-repeat报错

    [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq ...

  7. 关于在最新的 Visual Studio 2017 版本中使用 Web Deploy 遇到的 SSL 连接错误

    错误信息: 无法完成向远程代理 URL 发送请求.请求被中止: 未能创建 SSL/TLS 安全通道. 原因分析: 最新版本的 Visual Studio 中,已经抛弃了 https 协议中旧版 SSL ...

  8. webpack的配置文件

    1.路径 __dirname是一个全局变量,在项目html和js中没有单独定义,直接写就可以得到项目的根目录的路径 module.exports={ // __dirname是nodejs里的一个全局 ...

  9. ffmpeg-hls实现分析

    [时间:2018-05] [状态:Open] [关键词:流媒体,stream,HLS, ffmpeg, demuxer,avformat] 0 引言 本文将关注于FFmpeg中的HLS相关实现,相关代 ...

  10. @Resource注入为null

    UserController中userService为null只有一个原因, 那就是你所使用的UserController对象没有被IoC容器所管理,你使用的对象是自己new出来的, 而不由sprin ...