由于在 运行 方法中加载上下文 xml 时缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean while loading the context xml in run method
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class InitService extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run("classpath:abc-server.xml", args);
}
}
++++++++++++++++++++++++++++++++++++++++++++
我在这里尝试将 Spring MVC 项目迁移到 Spring 带有嵌入式 tomcat 的引导独立 jar。所以我尝试加载现有项目中使用的上下文 xml(abc-server.xml) 。当我 run/deploy 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
当您调用 运行 时,您只是提供 server-abc.xml
作为应用程序配置的来源:
SpringApplication.run("classpath:abc-server.xml", args);
这意味着 InitService
被忽略,包括您已启用自动配置这一事实。如果没有打开自动配置,Spring Boot 将不会自动为您配置嵌入式 servlet 容器。您需要提供 InitService
和 abc-server.xml
作为应用程序的配置。
我会向 SpringApplication.run
提供 InitService.class
并使用 @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
并删除其他三个注释,就像我在上面所做的那样。
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class InitService extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run("classpath:abc-server.xml", args);
}
}
++++++++++++++++++++++++++++++++++++++++++++
我在这里尝试将 Spring MVC 项目迁移到 Spring 带有嵌入式 tomcat 的引导独立 jar。所以我尝试加载现有项目中使用的上下文 xml(abc-server.xml) 。当我 run/deploy 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
当您调用 运行 时,您只是提供 server-abc.xml
作为应用程序配置的来源:
SpringApplication.run("classpath:abc-server.xml", args);
这意味着 InitService
被忽略,包括您已启用自动配置这一事实。如果没有打开自动配置,Spring Boot 将不会自动为您配置嵌入式 servlet 容器。您需要提供 InitService
和 abc-server.xml
作为应用程序的配置。
我会向 SpringApplication.run
提供 InitService.class
并使用 @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
并删除其他三个注释,就像我在上面所做的那样。