ApplicationContextException:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext

Vij*_*tia 25 spring spring-batch spring-boot

我使用Spring启动编写了一个Spring批处理应用程序.当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行正常.但是,当我试图在Linux服务器上运行它时,它给了我以下异常

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException: 
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Run Code Online (Sandbox Code Playgroud)

以下是我运行它的方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log
Run Code Online (Sandbox Code Playgroud)

saj*_*jib 38

对于非Web应用程序,web application type在属性文件中禁用:

application.properties:

spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)

如果你使用application.yml然后添加:

  spring:
    main:
      web-application-type: none
Run Code Online (Sandbox Code Playgroud)

对于Web应用程序,*SpringBootServletInitializer*在主类中扩展.

@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(YourAppliationName.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)


Spe*_*ton 16

我的解决方案与不良依赖有关。我有:

<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>
Run Code Online (Sandbox Code Playgroud)

在我的 pom 中,我不得不注释掉排除以使其正常工作。出于某种原因,它必须寻找这个 tomcat 包。


小智 14

可能您缺少@SpringBootApplicationSpring Boot入门类。

@SpringBootApplication
public class LoginSecurityAppApplication {

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

}
Run Code Online (Sandbox Code Playgroud)


Vij*_*tia 13

解决方案是:

我明确下面的属性设置为noneapplication.yml文件中.

spring:
  main:
    web-application-type: none
Run Code Online (Sandbox Code Playgroud)


adr*_*rhc 10

您可能会在您的项目中使用它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您还必须添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

奇迹发生了:)

PS:那是因为当两者都可用时,Spring 将默认使用web-MVC而不是web-flux


Man*_*ath 7

在我的情况下,问题解决了从 spring-boot-starte-web 中注释 tomcat 依赖项排除

     <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> 
Run Code Online (Sandbox Code Playgroud)


Kap*_*pil 6

添加以下 bean 对我有用。

    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }
Run Code Online (Sandbox Code Playgroud)

我在SpringApplication.run(MyApplication.class, args);没有@SpringBootApplication注释的情况下运行非 web spring 应用程序。


arv*_*rya 6

要将 Spring boot wen 应用程序转换为独立应用程序:

  1. 配置 application.properties:
spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)
  1. 或者使用 NONE Web 上下文更新应用程序上下文。
ApplicationContext ctx = new SpringApplicationBuilder(MigrationRunner.class)
                .web(WebApplicationType.NONE).run(args);
Run Code Online (Sandbox Code Playgroud)

使用应用程序上下文,您可以获得您的bean:

myBean bean = (MyBean) ctx.getBean("myBean", MyBean.class); bean.call_a_method(..);


Rav*_*ila 5

注释类public static void main,例如:@SpringBootApplication


归档时间:

查看次数:

63613 次

最近记录:

5 年,9 月 前