We have a project in spring-integration that will work with either webflex or servlet implementations and this seems to work pretty well using the spring.main.web-application-type property set as a system property.
I'm now looking for how to choose the embedded web server at runtime.
The spring-boot documentation says that this is accomplished via dependency inclusion or exclusion with the pom.xml.
https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/howto-embedded-web-servers.html
What I'm looking for is to be able to start any of jetty, netty, undertow or tomcat from the same project/executable jar.
Is this feasible by some specific startup sequence in SpringApplication?
Thanks for any pointers/suggestions.
I think that I've found a relatively simple and clean way to do this.
At Application context creation,
spring-bootloads theWebServerApplicationContextin one of two flavors, depending upon the value in propertyspring.main.web-application-type: eitherreactiveorservlet(respectivelyReactiveWebServerApplicationContextandServletWebServerApplicationContext).These two classes both have a method
createWebServer()which will look for a bean of type (respectively)ReactiveWebServerFactoryorServletWebServerFactoryloaded into the context, in which case this bean will be theWebServerFactoryused by theWebApplicationContext.Hence need to declare the target
WebServerFactoryas a bean.There are a number of different ways of doing this, including using profiles.
I've chosen to use a factory bean that instantiates the
WebServerFactoryas a function of a system property, heremy.main.web-server(as a parallel tospring.main.web-application-type).Using a
BeanFactoryto create the bean that holds theWebServerFactory:Using XML context definitions, I define the BeanFactory and import a source which will be different for
reactiveandservlet:Finally, create the
WebServerFactorywithin the specific source file. Withinmy-reactive.xml:and within
my-servlet.xml:I suppose that you don't necessarily need to have the specialized import because you can create both the
ReactiveWebServerFactoryand theServletWebServerFactorywithin the context but only the matching bean will be used.