0

我有两个小应用程序,一个用途spring-boot-starter-amqp,其他用途spring-data-hadoop-boot。我可以单独运行它们而没有任何问题。

当我将它们连接在一起时,应用程序启动失败并出现异常:org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

我的主要课程非常通用,并且分别适用于它们:

@PropertySource("file:conf/app.properties")
@SpringBootApplication
public class Job {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Job.class, args);
    }
}

我在这里迷路了。AFAIK@SpringBootApplication包含所需的所有注释,包括自动配置和组件扫描。我不需要配置 Web 环境,因为我没有使用它。当两个依赖项都在类路径中时,为什么我需要这样做,我该如何解决?

更新

我在 Spring Boot 代码中挖了一点。主要问题是SpringApplication.deduceWebEnvironment()根据类路径中某些类的存在自动检测应该配置什么样的环境。

对于 web 环境,正在检查两个类。当它们都在类路径中时,显然会检测到需要正确配置的 Web 环境。

  • javax.servlet.Servlet
  • org.springframework.web.context.ConfigurableWebApplicationContext

spring-boot-starter-amqp:1.3.1.RELEASEcontainsConfigurableWebApplicationContextspring-data-hadoop-boot:2.3.0.RELEASE-cdh5contains Servlet(在本机 Hadoop 库中)。

现在,当单独运行时,在这两种情况下都缺少上述类之一,从而导致未设置 Web 环境。

但是当我同时使用它们时 - 两个类都可以找到。检测到 Web 环境,误报,它需要配置,我无法(也不想)提供。

所以现在的问题是 - 我可以强制非 Web 环境,即使我在类路径中有这些类?或者有没有其他方法可以解决这个问题?(除了将它们从 Gradle 依赖项中排除)

4

1 回答 1

0

解决了。

遵循这个问题:如何防止 spring-web 的 spring-boot 自动配置?

我按如下方式运行应用程序。

@PropertySource("file:conf/app.properties")
@SpringBootApplication
public class Job {
    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(Job.class).web(false).run(args);
    }
}

上述问题的答案也建议使用 propertyspring.main.web_environment=false或 annotation @EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)。两种解决方案都对我不起作用。在我的情况下,只有编程解决方案才有效。

于 2016-01-21T06:35:43.390 回答