且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-01-07 17:55:34

当你调用 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.