我有一个使用Spring4.x的应用程序,并决定将其迁移到Spring5.x和spring boot 2.3.0。
该应用程序运行在jetty服务器上,该服务器有一些特殊的配置和相当多的连接器。我设法从spring获得了嵌入式jetty服务器并添加了连接器,但我正在努力设置一些特定的值。
我还将xml配置的一部分移到了带注释的类中-所以让我来解释一下这个问题。
如果我理解正确的话,我的配置类应该实现这些接口:
@Configuration
public class Server implements WebServerFactoryCustomizer<JettyServletWebServerFactory>,
ServletContextInitializer我在WebServerFactoryCustomizer中实现了void customize(JettyServletWebServerFactory factory)方法,在那里我添加了连接器并设置了一些值。
它看起来像这样:
void customize(JettyServletWebServerFactory factory) {
factory.addServerCustomizers(jettyServer -> {
jettyServer.addConnector(new ServerConnector(server, sslContextFactory));
...
// ---the part of the old code---
final ServletHolder servletHolder = new ServletHolder(new CXFServlet());
final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context .setContextPath("/");
context .addServlet(servletHolder, "/*");
context .addFilter(MyCustomFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
context .addEventListener( new ContextLoaderListener() );
// the problematic line
restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");
// ---
}
}问题是我把所有的配置都从XML转移到了带注释的类中,我不知道如何将initParameter指向我的带注释的类,这个类包含了所有的注释,比如SpringBootApplication、ComponentScan等等。
所以我在想,我会把这个(和其他一堆东西)从ServletContextInitializer void onStartup(ServletContext servletContext)转移到实现和覆盖的方法上,但是javax.servlet.ServletContext的参数和org.eclipse.jetty.servlet.ServletContextHandler不太一样,它为我提供了更多的选项,比如设置SessionHandler,ResourceHandler,ContextHandler……(我可以为ServletContext设置一些值,但不是全部...)
任何能为我指明正确方向的帮助都将不胜感激。
发布于 2020-06-06 02:19:43
所以我想通了..。对于基于XML的配置,您只需设置以下内容:
restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");对于基于类的,您必须设置以下两项:
restContext.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
restContext.setInitParameter("contextConfigLocation", Main.class.getName());https://stackoverflow.com/questions/62216282
复制相似问题