This is really simple tutorial of Spring Boot, starting with gradle.
Environment
gradle 2.10
Ubuntu 16.04.1 LTS
I used Ubuntu, but it’s the same in Mac and Windows, I think.
gradle init
With the following command, create gradle project
After that, files build.gradle
, gradle
, gradlew
, gradlew.bat
, settings.gradle
are created.
Spring Boot Basic
Write basic component into build.gradle
, which is created just before. The sample of build.gradle
is written in Spring Boot Reference Guide 10.1.2 Gradle installation . And I write the Kotlin version below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
buildscript {
repositories {
jcenter ( )
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath ( "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE" )
}
}
apply plugin : 'java'
apply plugin : 'spring-boot'
jar {
baseName = 'myproject'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter ( )
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile ( "org.springframework.boot:spring-boot-starter-web" )
testCompile ( "org.springframework.boot:spring-boot-starter-test" )
}
And, write main method. Create class Application in src/main/java/com/example/myproject/Application.java
, and write below contents. The sample contents is also written in Spring Boot Reference Guide .
package com . example . myproject ;
import org . springframework . boot . SpringApplication ;
import org . springframework . boot . autoconfigure . EnableAutoConfiguration ;
import org . springframework . context . annotation . ComponentScan ;
import org . springframework . context . annotation . Configuration ;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main ( String [ ] args ) {
SpringApplication . run ( Application . class , args ) ;
}
}
It is the most simple form. If you want to create Deployable War, you need some modification. Here, you can execute ./gradlew bootrun
and built-in tomcat will boot but nothing happen because we don’t write any meaningful codes.
We named first class “Application”, but you can change the name to whatever you like.
In Kotlin
It’s similar in Kotlin and Spring. In this case, modify build.gradle
a bit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
buildscript {
ext . kotlin_version = '1.0.0'
ext . spring_boot_version = '1.3.0.RELEASE'
repositories {
jcenter ( )
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath ( "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE" )
}
}
apply plugin : 'kotlin'
apply plugin : 'spring-boot'
jar {
baseName = 'myproject'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter ( )
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile ( "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" )
compile ( "org.springframework.boot:spring-boot-starter-web" )
testCompile ( "org.springframework.boot:spring-boot-starter-test" )
}
Application クラス (src/main/java/com/example/myproject/Application.kt) は次のようにします。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com . example . myproject
import org . springframework . boot . SpringApplication
import org . springframework . boot . autoconfigure . EnableAutoConfiguration
import org . springframework . context . annotation . ComponentScan
import org . springframework . context . annotation . Configuration
@Configuration
@EnableAutoConfiguration
@ComponentScan
open class Application {
companion object {
@JvmStatic fun main ( args : Array <String> ) {
SpringApplication . run ( Application :: class . java , * args )
}
}
}
There are other pattern of Application
class for Spring in Kotlin, and those are referred in Kotlin document.
The code I created this time is registered to GitHub .
This time we created extremely simple Spring Boot application.
Readers who viewed this page, also viewed:
Post navigation
A Life Summary of an Gypsy