kotlin之springboot小试牛刀

来源:互联网 发布:数码暴龙网络侦探骇客 编辑:程序博客网 时间:2024/06/10 07:08

kotlin今年大火,原因大家都知道的,正巧手里有个物料系统的小项目就拿来试着写一写。竟一发不可收拾,喜欢上了这门语言。其特性这里就不在废话了,主要还是喜欢它的简洁,快速。
这里我先配合springboot的搭建熟悉一下kotlin,语法的话准备专门整一个篇幅来说。因为是图文,有些细节就不展示了,我会尽量多作说明。

初建项目

可以在build.gradle看到idea已经自动配置好了。版本这块可能有不一致的情况,我就以我本地为准了。官方说明可以点这里

group 'com.mcp'version '1.0-SNAPSHOT'buildscript {    ext.kotlin_version = '1.1.4-3'    repositories {        mavenCentral()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}apply plugin: 'kotlin'repositories {    mavenCentral()}dependencies {    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"}compileKotlin {    kotlinOptions.jvmTarget = "1.8"}compileTestKotlin {    kotlinOptions.jvmTarget = "1.8"}

既然要搭建springboot,自然就需要我们引入jar包,我们可以先创建一个gradle.properties文件,一些额外的配置可以放在这里,内容如下:

springVersion=1.5.7.RELEASE

下一步在build.gradle增加配置:

group 'com.mcp'version '1.0-SNAPSHOT'buildscript {    ext.kotlin_version = '1.1.4-3'    repositories {        mavenCentral()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springVersion}"    }}apply plugin: 'kotlin'apply plugin: 'java'apply plugin: 'org.springframework.boot'repositories {    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }    maven { url "https://jitpack.io" }    maven { url "http://repo.maven.apache.org/maven2" }}sourceCompatibility = 1.8dependencies {    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'    compile group: 'org.springframework.boot', name: 'spring-boot-starter-test'}compileKotlin {    kotlinOptions.jvmTarget = "1.8"}compileTestKotlin {    kotlinOptions.jvmTarget = "1.8"}

创建目录和启动类,以及springboot的配置信息

Application.kt

package com.mcpimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication/** * Created by shiqm on 2017-10-27. */@SpringBootApplicationopen class Applicationfun main(args: Array<String>) {    SpringApplication.run(Application::class.java, *args)}

resources资源路径下添加配置文件
application.properties:

spring.profiles.active=dev

application-dev.properties:

#serverspring.application.name=springboot-kotlinserver.context-path=/server.tomcat.uri-encoding=UTF-8server.tomcat.compression=offserver.port=8080

最后直接在Application.kt运行即可

这里写图片描述

现在项目已经跑起来了,可以看到在Application 中代码极其简单,后面继续在这个基础上,把五脏六腑拼出来。

原创粉丝点击