Springboot:启动问题(缺少ServletWebServerFactory bean)

时间:2018-07-08 08:50:10

标签: java spring maven spring-boot

我在主类上添加了注释,如下所示:

package com.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;

import com.controller.RequestClass;

@CrossOrigin(origins = "http://localhost:4200")
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.*")
public class MainClass
{
    public static void main(String[] args)
    {
        SpringApplication.run(RequestClass.class, args);
    }
}

但是在运行应用程序时,遇到以下错误。

请告诉我是否需要其他文件进行调试

  

2018-07-08 14:16:27.321 INFO 7832 --- [主]   com.main.MainClass:在以下位置启动MainClass   带有PID 7832的DESKTOP-551C51M   (C:\ Users \ sparsh \ eclipse-workspace \ firstprogram \ target \ classs已启动   由sparsh在C:\ Users \ sparsh \ eclipse-workspace \ firstprogram)   2018-07-08 14:16:27.327 INFO 7832 --- [主]   com.main.MainClass:未设置有效的配置文件,   还原到默认配置文件:默认2018-07-08 14:16:27.382   INFO 7832 --- [主]   ConfigServletWebServerApplicationContext:刷新   org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2893de87:   启动日期[2018年7月8日星期日14:16:27 IST];上下文层次结构的根   2018-07-08 14:16:27.611 WARN 7832 --- [主]   ConfigServletWebServerApplicationContext:遇到异常   在上下文初始化期间-取消刷新尝试:   org.springframework.context.ApplicationContextException:无法执行   启动Web服务器;嵌套异常为   org.springframework.context.ApplicationContextException:无法执行   由于缺少启动ServletWebServerApplicationContext   ServletWebServerFactory bean。 2018-07-08 14:16:27.974错误7832 --- [   main] o.s.boot.SpringApplication:应用程序运行   失败

     

org.springframework.context.ApplicationContextException:无法执行   启动Web服务器;嵌套异常为   org.springframework.context.ApplicationContextException:无法执行   由于缺少启动ServletWebServerApplicationContext   ServletWebServerFactory bean。在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)   〜[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)   〜[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)   [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]位于   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)   [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:327)   [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)   [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]位于   org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)   [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]位于   com.main.MainClass.main(MainClass.java:21)[classes /:na]原因:   org.springframework.context.ApplicationContextException:无法执行   由于缺少启动ServletWebServerApplicationContext   ServletWebServerFactory bean。在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204)   〜[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)   〜[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]在   org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)   〜[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] ... 8个常见框架   省略

1 个答案:

答案 0 :(得分:1)

如果springapplicatiom.run是针对主类而不是控制器类的,那会发生什么?看看这个线程https://stackoverflow.com/a/44466367/5126865(我假设您想实现的目标很抱歉,如果道歉了)