由于在run方法中加载上下文xml时缺少EmbeddedServletContainerFactory bean,因此无法启动EmbeddedWebApplicationContext

时间:2015-05-19 10:33:35

标签: spring spring-boot

@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启动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

1 个答案:

答案 0 :(得分:2)

当您致电运行时,您只提供 echo $this->getLayout()->createBlock('cms/block') ->setBlockId('idOfMyCmsPage') ->toHtml(); 作为应用程序配置的来源:

server-abc.xml

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

我会向abc-server.xml提供InitService.class并使用SpringApplication.run来提取旧的XML配置:

@ImportResource

请注意,@SpringBootApplication @ImportResource("classpath:abc-server.xml") public class InitService { public static void main(String[] args) { SpringApplication.run(InitService.class, args); } } 相当于@SpringBootApplication@ComponentScan@Configuration。您可以使用@EnableAutoConfiguration并删除其他三个注释,如上所述。