我正在尝试通过添加一些即席执行器@RestController来增强Spring版本的apache-james。
为此,我想启动一个嵌入式Tomcat9.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应用程序就可以正常工作(至少在链接的示例中是这样)。
我遗漏了什么?
发布于 2021-06-10 14:19:29
如果在具有@EnableWebMvc的类之前声明了实现WebApplicationInitializer接口的类,则一切都会按预期进行。特别是,甚至没有必要将后者添加到上下文中,因为它已经在WebApplicationInitializer中启动的上下文中注册了
https://stackoverflow.com/questions/67912723
复制相似问题