Java EE graduated and officially enrolled in Spring: laughing: That's why I'm going to live a Spring life both publicly and privately: clap:
Now, back to the main subject, I think many of you are familiar with the relationship between Kotlin and Spring. Starting with Spring 5.0, Kotlin support has been taken in the core part of the framework.
Introducing Kotlin support in Spring Framework 5.0
Kotlin As a subject, I will touch on Spring in Kotlin's manners.
First, use SPRING INITIALIZR to create a template for Spring project and compare Java / Kotlin.
By the way, ** Kotlin 1.3.0 ** was just released the other day (2018/10/30): clap :: clap: There are updates such as ** Coroutines ** becoming official, but that's another story.
By the way, the default Kotlin version was ** 1.2.70 ** (as of 2018/11/1) when generated by SPRING INITIALIZR.
Spring Dependencies
Import the template generated by SPRING INITIALIZR into IntelliJ and use it. As mentioned earlier, the Kotlin version has changed from ** 1.2.70 ** to ** 1.3.0 **.
SPRING INITIALIZR
SPRING INITIALIZR is an online tool that literally creates a template for your Spring project and makes it easy to get started. The operation is very simple, just select the following items from the screen.
--Project type: [Maven / Gradle] --Development language: [Java / Kotlin / Groovy] --Spring Boot version: <img width = "168" alt = "Spring Initializr 2018-11-02 00-13-42.png " src = "https://qiita-image-store.s3.amazonaws.com/0" / 127983 / da4999bc-d61c-360b-6f69-898595e990d9.png "> --Group: Root package name --Artifact: Project name --Dependencies: Dependent components --This time ** Web ** is added
Click ** Generate Project ** for both Java / Kotlin to generate and download the Spring project template. Import the template into the IDE and use it.
Let's compare the build.gradle for the Java project and the build.gradle for the Kotlin project described below.
Of course, you can see that the description contents and format of both are basically the same. The differences are the following two points.
--Applicable plugin --Dependent libraries
For plugins, the Kotlin project applies the Kotlin plugin instead of the Java plugin.
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
Similarly, for dependent libraries, Kotlin libraries have been added.
implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
The fact that the standard library I'm adding is * jdk * instead of * jre * is another story. In any case, specify ** jdk **.
Java
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'io.pivotal.syanagihara'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Kotlin
buildscript {
ext {
kotlinVersion = '1.3.0'
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'io.pivotal.syanagihara'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
The comparison of Main class is exactly the same in Java / Kotlin. The only difference is whether the format of the Main method is Java style or Kotlin Kotlin style.
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class, args);
}
fun main(args: Array<String>) {
runApplication<SimpleApplication>(*args)
}
In Kotlin, arrays in Java do not exist. Therefore, we will use an Array object to represent the array.
The difference between Java arrays and Kotlin arrays is not so much the way they are defined, but the nature.
--Java array: covariant --Does not consider inheritance relationships between type parameters --Kotlin array: immutable --Consider inheritance relationships between type parameters
For example, Kotlin cannot assign Array
There is a description of the following parameters that cannot be seen in Java.
*args
This is the format used when expanding and using the elements of an array.
Java
package io.pivotal.syanagihara.simple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class, args);
}
}
Kotlin
package io.pivotal.syanagihara.simple
import org.springframework.boot.runApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class SimpleApplication
fun main(args: Array<String>) {
runApplication<SimpleApplication>(*args)
}
This time, I just compared the projects generated by SPRING INITIALIZR. However, I think that Kotlin seems to be able to use Spring as well as Java. I think you can understand that almost the same content was written.
It is understandable that it is difficult to identify by this alone because it is only the source immediately after automatic generation.
So, I would like to dig deeper into ** Spring loves Kotlin ** in the future.
Recommended Posts