package com.company.tobispringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TobiSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(TobiSpringBootApplication.class, args);
}
}
package com.company.tobispringboot;
public class TobiSpringBootApplication {
public static void main(String[] args) {
System.out.println("Hello Containerless Standalone Spring Boot!");
}
}
Hello Containerless Standalone Spring Boot!
가 찍히게 된다.
목표
코드
package com.company.tobispringboot;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
public class TobiSpringBootApplication {
public static void main(String[] args) {
ServletWebServerFactory factory = new TomcatServletWebServerFactory();
WebServer webServer = factory.getWebServer();
webServer.start();
}
}
Tomcat
을 통해 start 를 해도 되지만,, Tomcat 을 구동시키기 위한 별도의 설정이 필요하기에,
자바에서 구동을 쉽게 하도록 ServletWebServerFactory
로 이미 추상화를 시켜놓음
구현체는 Jetty Tomcat Undertow 가 존재한다
Factory 의 getWebServer 를 통해 현재 내가 구동할 Servlet 서버에 대한 정보를 불러온다.
이후 start() 하면
@Override
public void start() throws WebServerException {
synchronized (this.monitor) {
if (this.started) {
return;
}
try {
addPreviouslyRemovedConnectors();
Connector connector = this.tomcat.getConnector();
if (connector != null && this.autoStart) {
performDeferredLoadOnStartup();
}
checkThatConnectorsHaveStarted();
this.started = true;
logger.info("Tomcat started on port(s): " + getPortsDescription(true) + " with context path '"
+ getContextPath() + "'");
}
catch (ConnectorStartFailedException ex) {
stopSilently();
throw ex;
}
catch (Exception ex) {
PortInUseException.throwIfPortBindingException(ex, () -> this.tomcat.getConnector().getPort());
throw new WebServerException("Unable to start embedded Tomcat server", ex);
}
finally {
Context context = findContext();
ContextBindings.unbindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());
}
}
}