1. Overview
1.概述
In this quick tutorial, we’ll have a look at how to replace the EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2.
在这个快速教程中,我们将看看如何替换Spring Boot 2中的EmbeddedServletContainerCustomizer和ConfigurableEmbeddedServletContainer。
These classes were part of previous versions of Spring Boot, but have been removed starting with Spring Boot 2. Of course, the functionality is still available through the interface WebServerFactoryCustomizer and the class ConfigurableServletWebServerFactory.
这些类是Spring Boot以前版本的一部分,但从Spring Boot 2开始已经被移除。当然,这些功能仍然可以通过接口WebServerFactoryCustomizer和类ConfigurableServletWebServerFactory获得。
Let’s have a look at how to use these.
让我们来看看如何使用这些。
2. Prior to Spring Boot 2
2.在Spring Boot 2之前
First, let’s have a look at a configuration that uses the old class and interface and that we’ll need to replace:
首先,让我们看看一个使用旧的类和接口的配置,我们需要替换它。
@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8080);
container.setContextPath("");
}
}
Here, we’re customizing the servlet container’s port and context path.
在这里,我们要定制Servlet容器的端口和上下文路径。
Another possibility to achieve this is to use more specific sub-classes of ConfigurableEmbeddedServletContainer, for a container type such as Tomcat:
实现这一目标的另一种可能性是使用ConfigurableEmbeddedServletContainer的更具体的子类,用于Tomcat这样的容器类型。
@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory tomcatContainer =
(TomcatEmbeddedServletContainerFactory) container;
tomcatContainer.setPort(8080);
tomcatContainer.setContextPath("");
}
}
}
3. Upgrade to Spring Boot 2
3.升级到Spring Boot 2
In Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer, while the ConfigurableEmbeddedServletContainer class is replaced with ConfigurableServletWebServerFactory.
在Spring Boot 2中,EmbeddedServletContainerCustomizer接口被WebServerFactoryCustomizer取代,ConfigurableEmbeddedServletContainer类被ConfigurableServletWebServerFactory取代。
Let’s rewrite the previous example for a Spring Boot 2 project:
让我们为一个Spring Boot 2项目重写前面的例子。
public class CustomContainer implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8080);
factory.setContextPath("");
}
}
And the second example will now use a TomcatServletWebServerFactory:
而第二个例子现在将使用一个TomcatServletWebServerFactory:。
@Component
public class CustomContainer implements
WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setContextPath("");
factory.setPort(8080);
}
}
Similarly, we have the JettyServletWebServerFactory and UndertowServletWebServerFactory as equivalents for the removed JettyEmbeddedServletContainerFactory and UndertowEmbeddedServletContainerFactory.
同样,我们有JettyServletWebServerFactory和UndertowServletWebServerFactory作为被删除的JettyEmbeddedServletContainerFactory和UndertowEmbeddedServletContainerFactory的等同物。
4. Conclusion
4.总结
This short write-up showed how to fix an issue we might encounter when upgrading a Spring Boot application to version 2.x.
这篇短文展示了如何解决我们在将Spring Boot应用升级到2.x版本时可能遇到的问题。
An example of a Spring Boot 2 project is available in our GitHub repository.
在我们的GitHub资源库中,有一个Spring Boot 2项目的例子。