import org.springframework.boot.web.servlet.ServletContextInitializer
import org.springframework.boot.web.servlet.server.ServletWebServerFactory
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext
import org.springframework.web.servlet.DispatcherServlet
import kotlin.reflect.KClass
class MySpringApplication(
) {
companion object {
fun run(
applicationClass: KClass<*>,
args: Array<String>,
) {
val applicationContext = AnnotationConfigWebApplicationContext()
applicationContext.register(applicationClass.java)
applicationContext.refresh()
val serverFactory = applicationContext.getBean(ServletWebServerFactory::class.java)
val dispatcherServlet = applicationContext.getBean(DispatcherServlet::class.java)
val webServer = serverFactory.getWebServer(ServletContextInitializer {
it.addServlet("dispatcherServlet", DispatcherServlet(applicationContext)).addMapping("/*")
})
webServer.start()
}
}
}
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.DispatcherServlet
@Configuration
@ComponentScan
class DeepApplication {
@Bean
fun servletWebServerFactory() = TomcatServletWebServerFactory()
@Bean
fun dispatcherServlet() = DispatcherServlet()
}
fun main(args: Array<String>) {
MySpringApplication.run(DeepApplication::class, args)
}
예제 코드를 코틀린으로 변환했습니다.
마지막 코드는 부트 처음 생성 시 코드로 되돌아가면 됩니다.
fun main(args: Array<String>) {
runApplication<DeepApplication>(*args)
}