Skip to content

Commit ad810e1

Browse files
committed
Update with new section on alternative main function definition.
* Also, revert changes back to the previous, original version in the repository.
1 parent 44f7959 commit ad810e1

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

docs/tutorials/spring-boot-restful.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ sourceSets {
6565
}
6666
}
6767
68-
// tells spring boot to look for the main method under the default package
69-
springBoot {
70-
mainClass = '_DefaultPackage'
71-
}
72-
7368
dependencies {
7469
compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") // New
7570
compile("org.springframework.boot:spring-boot-starter-web")
@@ -107,14 +102,21 @@ public class GreetingController {
107102
As can be seen, this is again pretty much a one-to-one translation of Java to Kotlin, with nothing special required for Kotlin.
108103

109104
### 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.
111110

112111
``` kotlin
113112
ComponentScan
114113
EnableAutoConfiguration
115-
public class Application
116-
public fun main(args: Array<String>) {
117-
SpringApplication.run(javaClass<Application>(), *args)
114+
public class Application {
115+
class object {
116+
platformStatic public fun main(args: Array<String>) {
117+
SpringApplication.run(javaClass<Application>(), *args)
118+
}
119+
}
118120
}
119121
```
120122

@@ -126,3 +128,40 @@ We can now use the any of the standard Gradle tasks for Spring Boot to run the a
126128
the application is compiled, resources bundled and launched, allowing us to access is via the browser (default port is 8080)
127129

128130
![Running App]({{ site.baseurl }}/{{ site.img_tutorial_root }}/spring-boot-restful/running-app.png)
131+
132+
133+
### Alternative Application class definition
134+
135+
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:
137+
138+
```kotlin
139+
ComponentScan
140+
EnableAutoConfiguration
141+
public class Application
142+
143+
public fun main(args: Array<String>) {
144+
SpringApplication.run(javaClass<Application>(), *args)
145+
}
146+
```
147+
148+
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`:
158+
159+
```groovy
160+
springBoot {
161+
mainClass = '_DefaultPackage'
162+
}
163+
```
164+
165+
Finally, you can start again the application with
166+
167+
gradle bootRun

0 commit comments

Comments
 (0)