You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -107,14 +102,21 @@ public class GreetingController {
107
102
As can be seen, this is again pretty much a one-to-one translation of Java to Kotlin, with nothing special required for Kotlin.
108
103
109
104
### Creating the Application class
110
-
Finally we need to define an Application class. As Spring Boot looks for a public static main method, we need to define this in Kotlin.
105
+
Finally we need to define an Application class. As Spring Boot looks for a public static main method, we need to define this in Kotlin
106
+
using the *platformStatic* attribute. For this, we create a standard *Application* class and define a class object inside where we can then create
107
+
a function decorated with *platformStatic*
108
+
109
+
Note: platformStatic is an annotation in Kotlin which is used for interoperability with Java, so that the resulting method is defined as static when called from Java.
In Java, the `main()` method of a Spring Boot application is conventionally defined within the annotated application class. This is because Java *does not* support top-level methods.
136
+
In Kotlin, however, we *do* have [top-level functions]({{ site.baseurl }}/docs/functions.html). Thus, we can make the Spring main entry point much simpler:
The only requirement for this variant to work is to declare in your `build.gradle` file to look for *this* main function. This is done through the `mainClass` property of the `springBoot` section:
149
+
150
+
151
+
```groovy
152
+
springBoot {
153
+
mainClass = 'my.package.YourMainClass'
154
+
}
155
+
```
156
+
157
+
In Kotlin, top-level functions are compiled into static members of an automatically-generated class. The name of this class is derived from the name of the package. For instance, a top-level function in the `com.example` package would be defined in a class named `com.example.ExamplePackage`. In our case, because we did not declare a package explicitly, the function is defined in the so-called *default* package; thus, the special class `_DefaultPackage` is defined. You may add the following lines to your `build.gradle`:
0 commit comments