由于在 run 方法中加载上下文 xml 时缺少 EmbeddedServletContainerFactory bean,因此无法启动 EmbeddedWebApplicationContext [英] Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean while loading the context xml in run method

查看:91
本文介绍了由于在 run 方法中加载上下文 xml 时缺少 EmbeddedServletContainerFactory bean,因此无法启动 EmbeddedWebApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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 时,抛出以下异常.

Here i am trying to migrate the Spring MVC project to Spring boot Standalone jar with embedded tomcat. So i tried loading the context xml(abc-server.xml) used in the existing project. When i run/deploy the spring boot jar, the following exception is thrown.

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

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

[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

推荐答案

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

When you call run, you're only providing server-abc.xml as a source for your application's configuration:

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

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

That means that InitService is being ignored, including the fact that you've enabled auto-configuration. Without auto-configuration being switched on, Spring Boot will not automatically configure an embedded servlet container for you. You need to provide both InitService and abc-server.xml as configuration for your application.

我会向 SpringApplication.run 提供 InitService.class 并使用 @ImportResource 来引入你的旧 XML 配置:

I would provide InitService.class to SpringApplication.run and use @ImportResource to pull in your old XML configuration:

@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 并删除其他三个注释,就像我上面所做的那样.

Note that @SpringBootApplication is equivalent to @ComponentScan, @Configuration, and @EnableAutoConfiguration. You can just use @SpringBootApplication and drop the other three annotations as I've done above.

这篇关于由于在 run 方法中加载上下文 xml 时缺少 EmbeddedServletContainerFactory bean,因此无法启动 EmbeddedWebApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆