Spring with Kotorin ―― 1. SPRING INITIALIZR

Overview / Description

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.

Assumptions / Environment

Runtime version

By the way, the default Kotlin version was ** 1.2.70 ** (as of 2018/11/1) when generated by SPRING INITIALIZR.

Spring Dependencies

Development environment

Procedure / Explanation

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

Spring Initializr 2018-11-02 00-03-21.png

Click ** Generate Project ** for both Java / Kotlin to generate and download the Spring project template. Import the template into the IDE and use it.

This Java project

Spring Initializr 2018-11-01 21-50-43.png

This Kotlin project

Spring Initializr 2018-11-01 21-51-57.png

Compare build.gradle

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')
}

Comparison of Main class

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)
}

Array

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 used in the parameter of Main method to Array . You can keep the type safe and avoid the risk of run-time errors.

Array expansion

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)
}

Summary / Looking back

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.

This source

Recommended Posts

Spring with Kotorin ―― 1. SPRING INITIALIZR
Spring with Kotorin --- 5. Actuator
Spring with Kotorin --8 Repository layer
Spring with Kotorin ―― 7. Service layer
Spring with Kotorin --9 Database migration --Flyway
Spring with Kotorin --2 RestController and Data Class
Hello World at explosive speed with Spring Initializr! !! !!
Spring with Kotorin --8 Repository layer --Supplement: H2 Database
Self-made Validation with Spring
With Kotorin ―― 7. Scoping Function
Download with Spring Boot
Spring with Kotorin --3. Omitting curly braces from the function
Generate barcode with Spring Boot
Hello World with Spring Boot
Java Config with Spring MVC
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Login function with Spring Security
Using Mapper with Java (Spring)
Hello World with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Create microservices with Spring Boot
Send email with spring boot
Implemented authentication function with Spring Security ②
Implemented authentication function with Spring Security ③
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Implemented authentication function with Spring Security ①
Get validation results with Spring Boot
Implement file download with Spring MVC
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
[Spring] Autowired multiple beans. (With bonus)
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Authentication / authorization with Spring Security & Thymeleaf
Test Spring framework controller with Junit
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
OR search with Spring Data Jpa Specification
Hello World with Eclipse + Spring Boot + Maven
Create a simple on-demand batch with Spring Batch
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
DB authentication with Spring Security & hashing with BCrypt
Create Spring Boot-gradle-mysql development environment with Docker
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose