Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2

We are using the next interfaces in Spring Boot 1.5.9, working without any problem: ConfigurableEmbeddedServletContainer and EmbeddedServletContainerCustomizer.

We have upgraded to Spring Boot 2 (2.0.0.M7), and these interfaces don't exist. We think they have been modified for other interfaces with the same purpose but we don't know what they are.

Could somebody help us in order to know how to modify this piece of code for having the same behaviour we had in Spring 1.5.9?

The code we have is the next:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() throws FileNotFoundException {

    final TomcatConnectorCustomizer customizer = new MyTomcatConnectionCustomizer(absoluteKeystoreFile,
            keystoreType, keystorePassword);
    return new EmbeddedServletContainerCustomizer() {

        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
                containerFactory.addConnectorCustomizers(customizer);

                Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                connector.setPort(port);
                containerFactory.addAdditionalTomcatConnectors(connector);

            }
        }
    };
}
like image 627
Iván Avatar asked Dec 15 '17 13:12

Iván


People also ask

What is EmbeddedServletContainerCustomizer?

public interface EmbeddedServletContainerCustomizer. Strategy interface for customizing auto-configured embedded servlet containers. Any beans of this type will get a callback with the container factory before the container itself is started, so you can set the port, address, error pages etc.

What is TomcatEmbeddedServletContainerFactory?

public class TomcatEmbeddedServletContainerFactory extends AbstractEmbeddedServletContainerFactory implements ResourceLoaderAware. EmbeddedServletContainerFactory that can be used to create TomcatEmbeddedServletContainer s. Can be initialized using Spring's ServletContextInitializer s or Tomcat LifecycleListener s.

What is TomcatServletWebServerFactory?

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware. AbstractServletWebServerFactory that can be used to create TomcatWebServer s.

What is spring boot servlet initializer?

SpringBootServletInitializer is an interface to run SpringApplication from a traditional WAR deployment. It binds Servlet, Filter and ServletContextInitializer beans from the application context to the server.


1 Answers

You need to return an implementation of ConfigurableServletWebServerFactory as a bean, in your case TomcatServletWebServerFactory

@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
    final TomcatConnectorCustomizer customizer = new MyTomcatConnectionCustomizer(absoluteKeystoreFile,
        keystoreType, keystorePassword);
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(connector -> {
        connector.setPort(port);
    }, customizer);
    return factory;
}
like image 169
shazin Avatar answered Oct 07 '22 11:10

shazin