迁移Jetty到SpringBoot Jetty

huangapple go评论85阅读模式
英文:

Migrating Jetty to SpringBoot Jetty

问题

我有一个使用 Spring 4.x 的应用程序,并决定将其迁移到使用 Spring 5.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));
       ...

       // ---旧代码的部分---
       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() );

       // 问题所在的一行
       restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");

       // ---
   });
}

问题是,我将所有的配置从 XML 移动到了带注释的类中,但我不知道任何方式可以将 initParameter 指向包含所有注释的实现类,比如 SpringBootApplicationComponentScan 等等……

因此,我在考虑将这部分代码(以及其他一些代码)移到从 ServletContextInitializer 中实现和覆盖的方法 void onStartup(ServletContext servletContext) 中,但是 javax.servlet.ServletContext 参数与 org.eclipse.jetty.servlet.ServletContextHandler 不太相同,后者为我提供了更多选项,比如设置 SessionHandlerResourceHandlerContextHandler 等等(我能够将一些值设置到 ServletContext 中,但不是全部……)。

如果能够指导我朝正确的方向前进,我将不胜感激...

英文:

I have an app that uses spring 4.x, and decided to migrate it to spring 5.x with spring boot 2.3.0.

The app is running on top of the jetty server that has some special configuration and quite a few connectors. I managed to get the hold of the embedded jetty server from spring and add the connectors, but I am struggling with setting some of the specific values.

I also moved part of the xml configuration to annotated classes - so let me explain the problem.

If i understand correctly, my configuration class should implement these interfaces:

@Configuration
public class Server implements WebServerFactoryCustomizer&lt;JettyServletWebServerFactory&gt;,
							   ServletContextInitializer

I implement the void customize(JettyServletWebServerFactory factory) method from WebServerFactoryCustomizer where I add connectors and set some values.

That looks like this:

void customize(JettyServletWebServerFactory factory) {
   factory.addServerCustomizers(jettyServer -&gt; {
       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(&quot;/&quot;);
		context .addServlet(servletHolder, &quot;/*&quot;);
		context .addFilter(MyCustomFilter.class, &quot;/*&quot;, EnumSet.allOf(DispatcherType.class));
		context .addEventListener( new ContextLoaderListener() );

        // the problematic line
		restContext.setInitParameter(&quot;contextConfigLocation&quot;, &quot;classpath:application-context.xml&quot;);

       // ---
   }
}

The problem is I moved all the configuration from XML to annotated class, and I don;t know of any way how to point the initParameter to my annotated class which contains all of the annotations such as SpringBootApplication, ComponentScan, etc...

So I was thinking, I will move this (and bunch of other stuff) to implemented and overriden method from ServletContextInitializer void onStartup(ServletContext servletContext) but the javax.servlet.ServletContext argument is not quite the same as org.eclipse.jetty.servlet.ServletContextHandler which offers me more options like setting SessionHandler, ResourceHandler, ContextHandler... (I was able to set some of the values to the ServletContext but not all...)

Any help that'd point me to right direction is appreciated...

答案1

得分: 1

好的,以下是翻译好的部分:

Soo,我找到了解决方法...
对于基于XML的配置,您只需设置如下内容:

restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");

对于基于类的配置,您必须设置这两个内容:

restContext.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
restContext.setInitParameter("contextConfigLocation", Main.class.getName());
英文:

Soo I figured it out...
for XML based configuration you set just this:

restContext.setInitParameter(&quot;contextConfigLocation&quot;, &quot;classpath:application-context.xml&quot;);

For class based you must set these two:

	restContext.setInitParameter(&quot;contextClass&quot;, AnnotationConfigWebApplicationContext.class.getName());
	restContext.setInitParameter(&quot;contextConfigLocation&quot;, Main.class.getName());

huangapple
  • 本文由 发表于 2020年6月5日 21:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定