Spring 启动 AMQP 和 Spring Hadoop 一起以缺少 EmbeddedServletContainerFactory bean 结束
Spring boot AMQP and Spring Hadoop together ends up with missing EmbeddedServletContainerFactory bean
我有两个小应用程序,一个使用 spring-boot-starter-amqp
,另一个使用 spring-data-hadoop-boot
。我可以 运行 他们分开没有任何问题。
当我将它们连接在一起时,应用程序启动失败并出现异常:org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
。
我的主要 class 非常通用,并且分别对它们都适用:
@PropertySource("file:conf/app.properties")
@SpringBootApplication
public class Job {
public static void main(String[] args) throws Exception {
SpringApplication.run(Job.class, args);
}
}
我在这里迷路了。 AFAIK @SpringBootApplication
包含所有需要的注释,包括自动配置和组件扫描。我没有必要配置网络环境,因为我没有使用它。当两个依赖项都在 class 路径中时,为什么我需要这样做,我该如何修复它?
更新
我在 Spring 引导代码中挖掘了一点。主要问题是 SpringApplication.deduceWebEnvironment()
根据 class 路径中某些 classes 的存在自动检测应该配置什么样的环境。
对于 Web 环境,正在检查两个 class。当它们都在 class 路径中时,显然会检测到需要正确配置的 Web 环境。
javax.servlet.Servlet
org.springframework.web.context.ConfigurableWebApplicationContext
spring-boot-starter-amqp:1.3.1.RELEASE
包含 ConfigurableWebApplicationContext
,spring-data-hadoop-boot:2.3.0.RELEASE-cdh5
包含 Servlet
(在本机 Hadoop 库中)。
现在,单独使用运行时,上述classes之一在两种情况下都缺失,导致未设置网络环境。
但是当我同时使用它们时 - class 都可以找到。检测到 Web 环境,误报,它需要配置,我无法(也不想)提供。
所以现在的问题是——即使我在 class 路径中有那些 classes,我是否可以强制使用非 Web 环境?或者有没有其他方法可以解决这个问题? (除了将它们从 Gradle 依赖项中排除)
已解决。
关注这个问题:
本人运行申请如下
@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);
}
}
上述问题的答案也建议使用属性 spring.main.web_environment=false
或注解@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
。这两种解决方案都不适合我。只有编程解决方案适用于我的情况。
我有两个小应用程序,一个使用 spring-boot-starter-amqp
,另一个使用 spring-data-hadoop-boot
。我可以 运行 他们分开没有任何问题。
当我将它们连接在一起时,应用程序启动失败并出现异常:org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
。
我的主要 class 非常通用,并且分别对它们都适用:
@PropertySource("file:conf/app.properties")
@SpringBootApplication
public class Job {
public static void main(String[] args) throws Exception {
SpringApplication.run(Job.class, args);
}
}
我在这里迷路了。 AFAIK @SpringBootApplication
包含所有需要的注释,包括自动配置和组件扫描。我没有必要配置网络环境,因为我没有使用它。当两个依赖项都在 class 路径中时,为什么我需要这样做,我该如何修复它?
更新
我在 Spring 引导代码中挖掘了一点。主要问题是 SpringApplication.deduceWebEnvironment()
根据 class 路径中某些 classes 的存在自动检测应该配置什么样的环境。
对于 Web 环境,正在检查两个 class。当它们都在 class 路径中时,显然会检测到需要正确配置的 Web 环境。
javax.servlet.Servlet
org.springframework.web.context.ConfigurableWebApplicationContext
spring-boot-starter-amqp:1.3.1.RELEASE
包含 ConfigurableWebApplicationContext
,spring-data-hadoop-boot:2.3.0.RELEASE-cdh5
包含 Servlet
(在本机 Hadoop 库中)。
现在,单独使用运行时,上述classes之一在两种情况下都缺失,导致未设置网络环境。
但是当我同时使用它们时 - class 都可以找到。检测到 Web 环境,误报,它需要配置,我无法(也不想)提供。
所以现在的问题是——即使我在 class 路径中有那些 classes,我是否可以强制使用非 Web 环境?或者有没有其他方法可以解决这个问题? (除了将它们从 Gradle 依赖项中排除)
已解决。
关注这个问题:
本人运行申请如下
@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);
}
}
上述问题的答案也建议使用属性 spring.main.web_environment=false
或注解@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
。这两种解决方案都不适合我。只有编程解决方案适用于我的情况。