【问题标题】:Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean while loading the context xml in run method由于在运行方法中加载上下文 xml 时缺少 EmbeddedServletContainerFactory bean,因此无法启动 EmbeddedWebApplicationContext
【发布时间】:2015-07-31 03:14:33
【问题描述】:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication

public class InitService extends SpringBootServletInitializer { 

    public static void main(String[] args) {
    SpringApplication.run("classpath:abc-server.xml", args);
    }
}

++++++++++++++++++++++++++++++++++++++++++++

在这里,我正在尝试将 Spring MVC 项目迁移到带有嵌入式 tomcat 的 Spring boot Standalone jar。所以我尝试加载现有项目中使用的上下文 xml(abc-server.xml)。当我运行/部署 spring boot jar 时,会抛出以下异常。

++++++++++++++++++++++++++++++++++++++++

[2015-05-19 15:12:30,012] ERROR org.springframework.boot.SpringApplication  - Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is     org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.gogo.asp.server.init.InitService.main(InitService.java:188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
... 13 more

【问题讨论】:

  • 我必须加载上下文 xml、servlet xml 和 spring 上下文 xml。我尝试了很多方法。请帮助我。
  • 如果有人能提出在 WebApplicationContext 中加载上下文 xml 的替代方法,那就太好了

标签: spring spring-boot


【解决方案1】:

当您调用 run 时,您只是提供 server-abc.xml 作为应用程序配置的来源:

SpringApplication.run("classpath:abc-server.xml", args);

这意味着InitService 将被忽略,包括您已启用自动配置这一事实。如果没有打开自动配置,Spring Boot 不会自动为您配置嵌入式 servlet 容器。您需要同时提供 InitServiceabc-server.xml 作为应用程序的配置。

我会将InitService.class 提供给SpringApplication.run 并使用@ImportResource 提取您的旧XML 配置:

@SpringBootApplication
@ImportResource("classpath:abc-server.xml")
public class InitService {

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

请注意,@SpringBootApplication 等效于 @ComponentScan@Configuration@EnableAutoConfiguration。您可以使用@SpringBootApplication 并像我在上面所做的那样删除其他三个注释。

【讨论】:

  • 感谢安迪·威尔金森 :)
  • 在现有的代码库中,我们使用了 org.springframework.context.support.AbstractRefreshableConfigApplicationContext 和 org.springframework.context.support.AbstractApplicationContext 类。因此,如果我使用 @ImportResource() ,我会收到此异常“org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.context.support.AbstractRefreshableConfigApplicationContext] 找到依赖项:预期至少 1有资格作为此依赖项的自动装配候选者的 bean"
  • 这是一个相当奇怪的依赖注入。您真的需要访问该特定类型吗?为什么不能自动装配ApplicationContext 或实现ApplicationContextAware
  • 我正在将项目迁移到 Spring Boot,我不知道,为什么他们使用 AbstractRefreshableConfigApplicationContext‌AbstractApplicationContext。那么我可以用ApplicationContext 替换这些类吗?会不会有什么问题?
  • 我不知道为什么会出现异常,尽管我妥善保存了必要的罐子。如果我上相同的课程,那将很容易迁移。
猜你喜欢
  • 2015-03-19
  • 2016-05-06
  • 1970-01-01
  • 2016-03-05
  • 2014-07-28
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 2014-03-14
相关资源
最近更新 更多