【问题标题】:Spring Boot: Running as a Java application but classpath contains spring-webSpring Boot:作为 Java 应用程序运行,但类路径包含 spring-web
【发布时间】:2016-01-08 21:01:35
【问题描述】:

我已经成功地创建了一个作为 Java 应用程序运行的 Spring Boot 应用程序,如下所示:

@SpringBootApplication
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication springApplication = new SpringApplication();
    springApplication.setWebEnvironment(false);
    springApplication.run(Application.class, args);
}

问题是我的应用程序需要 spring-web 模块,因为它是 REST 服务的客户端。

一旦我添加了spring-web 模块,我就会收到一个错误:

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

如何让它作为 Java 应用程序在类路径上使用 spring-web 运行

【问题讨论】:

标签: spring-boot


【解决方案1】:

我和你有“相同的设置”——命令行 spring boot 应用程序使用来自 spring-webRestTemplate 并且一切正常。也许只是我使用“完整”的spring web starter。

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

(只是主线略有不同,但应该没有区别)

    SpringApplication app = new SpringApplication(MyApplication.class);
    app.setWebEnvironment(false);
    app.run(args);

【讨论】:

  • app.run(args) 是这里的关键,通过包含我的 Application.class 它正在创建默认配置并忽略我的设置,因此通过排除它现在作为 Java 进程运行。谢谢!
  • hm,我没想到这么简单的问题 -> 以非静态方式调用静态方法。你的 IDE 可能警告过你 :) 但很高兴它现在可以工作了。
  • 这不是静态方法,虽然还有另外一个静态run()方法,但我没用这个。
  • 在 1.3.1 版本中 SpringApplication 只有 3 个 run 方法 - 但只有一个 - run(String...) 是非静态的。但这只是细节:)
猜你喜欢
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
相关资源
最近更新 更多