Spring Boot application class

The following code block shows the generated SpringBootApplication class, FirstWebServiceWithKotlinApplication. We made the class open to enable Spring Boot to override it:

    @SpringBootApplication
open class FirstWebServiceWithKotlinApplication
fun main(args: Array<String>) {
SpringApplication
.run(
FirstWebServiceWithKotlinApplication::class.java,
*args)
}

The following are a few important things to note:

  • Package, import, and annotations are the same as that of a Java class.
  • The declaration of the main function in Java was public static void main(String[] args). In the preceding example, we are using the Kotlin function syntax. Kotlin does not have static methods. Any function declared outside of a class can be called without needing a class reference.
  • Launching SpringApplication in Java is done using SpringApplication.run(FirstWebServiceWithKotlinApplication.class, args).
  • :: is used to obtain a Kotlin class runtime reference. So, FirstWebServiceWithKotlinApplication::class gives us a runtime reference to the Kotlin class. To obtain a Java class reference, we need to use the .java property on the reference. So, in Kotlin, the syntax is FirstWebServiceWithKotlinApplication::class.java.
  • In Kotlin, * is called a spread operator. It is used when passing an array to a function accepting variable arguments. So, we will use *args to pass the array to the run method.

The application can be launched up by running FirstWebServiceWithKotlinApplication as a Kotlin application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset