21 Mayıs 2020 Perşembe

SpringBoot TomcatEmbeddedServletContainerFactory Sınıfı - Kullanmayın

Giriş
SpringBoot 2 ile bu sınıf yerine TomcatServletWebServerFactory geldi.

TomcatEmbeddedServletContainer nesnesi yaratır

addContextValve metodu
Şöyle yaparız.
TomcatEmbeddedServletContainerFactory tomcat = 

LogbackValve logbackValve = new LogbackValve();

logbackValve.setFilename("logback-access.xml");

tomcat.addContextValves(logbackValve);
getTomcatEmbeddedServletContainer metodu
Açıklaması şöyle.
By default, JNDI is disabled in embedded Tomcat which is causing the NoInitialContextException. You need to call Tomcat.enableNaming() to enable it. The easiest way to do that is with a TomcatEmbeddedServletContainer subclass
Şöyle yaparız.
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
  return new TomcatEmbeddedServletContainerFactory() {

    @Override
    protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
               Tomcat tomcat) {
      tomcat.enableNaming();
      return super.getTomcatEmbeddedServletContainer(tomcat);
    }
  };
}
setPort metodu
Tomcat'in dinleyeceği port'ları atamak için kullanılır.

Örnek
Şöyle yaparız.
TomcatEmbeddedServletContainerFactory tomcatFactory = ...;
Connector[] additionalConnectors = this.additionalConnector();
tomcatFactory.addAdditionalTomcatConnectors(additionalConnectors);

private Connector[] additionalConnector() {
  
  String[] ports = ...;
  List<Connector> result = new ArrayList<>();
    for (String port : ports) {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      connector.setScheme("http");
      connector.setPort(Integer.valueOf(port));
      result.add(connector);
    }
  }
  return result.toArray(new Connector[] {});
}
setSsl metodu
Örnek
Şöyle yaparız.
@Bean
public EmbeddedServletContainerFactory servletContainer() {
  TomcatEmbeddedServletContainerFactory tomcatFactory = new 
    TomcatEmbeddedServletContainerFactory();
  tomcatFactory.setSsl(createSsl());
  tomcatFactory.setPort(...);
  return tomcatFactory;
}
private Ssl createSsl() {
  Ssl ssl = new Ssl();
  try {
    ssl.setKeyStore("/path/to/keystore.pkcs12");
    ssl.setKeyStorePassword(new String(...));
    ssl.setKeyAlias(...);
  } catch (Exception e) {
    logger.error(e.getMessage());
  }
  return ssl;
}

Hiç yorum yok:

Yorum Gönder