【问题标题】:Embedded Tomcat in Apache JamesApache James 中的嵌入式 Tomcat
【发布时间】:2021-08-26 23:08:15
【问题描述】:

我正在尝试通过添加一些临时执行器 @RestControllers 来增强 apache-james 的 Spring 版本。
为此,我正在考虑启动一个嵌入式 Tomcat 9.x 服务器作为 sidecar,以便可以访问端点。

不幸的是,我收到了IllegalStateException: No ServletContext set 异常。

到目前为止我的代码如下:

/**
 * @author cdprete
 * @since 09.06.21
 */
public class EmbeddedTomcat implements DisposableBean {
    private final Tomcat tomcat;

    public EmbeddedTomcat() throws LifecycleException {
        String appBase = ".";
        tomcat = new Tomcat();
        tomcat.getConnector();
        tomcat.getHost().setAppBase(appBase);
        Context context = tomcat.addWebapp("", appBase);
        // Don't scan MANIFESTs, otherwise lots of JARs will be reported as missing
        ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
        tomcat.start();
    }
    @Override
    public void destroy() throws Exception {
        tomcat.destroy();
    }
}
/**
 * @author cdprete
 * @since 08.06.21
 */
@EnableWebMvc
@Configuration
@Import({HealthConfiguration.class, DiskSpaceConfiguration.class, PingConfiguration.class, TraceConfiguration.class})
public class ActuatorConfiguration implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(ActuatorConfiguration.class);
        ctx.setServletContext(servletContext);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

在主spring.xml 文件中:

<beans ...>
    ...

    <!-- Embedded Tomcat -->
    <bean class="ch.ti8m.channelsuite.james.actuator.EmbeddedTomcat" />

    <!-- Actuator configuration -->
    <bean class="ch.ti8m.channelsuite.james.actuator.ActuatorConfiguration" />
</beans>

如果我从 main 方法启动 Tomcat(例如,就像在 here 中一样),那么 Web 应用程序将按预期工作(至少是链接示例中的那个)。

我错过了什么?

【问题讨论】:

    标签: java spring spring-mvc tomcat apache-james


    【解决方案1】:

    如果实现WebApplicationInitializer 接口的类在具有@EnableWebMvc 的类之前声明,那么一切都会按预期进行。特别是,甚至没有必要将后者添加到上下文中,因为它已经在 WebApplicationInitializer 发起的上下文中注册了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2011-11-01
      • 2015-06-21
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多