基于gradle构建kotlin的springboot项目

章鱼哥springboot笔记之:kotlin的springboot项目搭建

废话不多说直接上代码:https://start.spring.io/

选择完后直接下载,idea导入

注意:导入的时候选择gradle项目

build.gradle文件如下

buildscript {
	ext {
		kotlinVersion = '1.2.51'
		springBootVersion = '2.0.5.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 = 'com.shuma'
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 {
	mavenLocal()
	mavenCentral()
}


dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('com.fasterxml.jackson.module:jackson-module-kotlin')
	compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
	compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	compile("org.jetbrains.kotlin:kotlin-reflect")
	compile group: 'com.alibaba', name: 'druid', version: '1.1.3'
	runtime('mysql:mysql-connector-java')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

kotlin版本hello word!

package com.shuma.kotlin.controller

import com.shuma.kotlin.model.User
import com.shuma.kotlin.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class TestController {

    @Autowired
    lateinit var userService :UserService

    @GetMapping("/hello")
    fun test():String{
        return "hello kotlin web!"
    }
}

main方法启动项目:http://127.0.0.1:8080/hello

亲测可用,githup地址:https://github.com/tzj1042/springBoot-kotlin

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kotlin构建Spring Boot Starter的步骤如下: 1. 创建一个Kotlin项目,可以使用IntelliJ IDEA或者其他IDE。 2. 添加Spring Boot和Spring Framework依赖,可以在build.gradle文件中添加以下依赖: ``` dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework:spring-context") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlin:kotlin-reflect") } ``` 3. 创建一个自动配置类,继承自Spring Boot的AutoConfiguration类,并在类上添加@Configuration注解。该类可以在Spring Boot应用程序启动时自动配置一些Bean。 ``` @Configuration class MyAutoConfiguration { @Bean fun myService(): MyService { return MyService() } } ``` 4. 创建一个启动器类,继承自Spring Boot的Starter类,并在类上添加@SpringBootConfiguration注解。该类可以将自动配置类与应用程序关联起来。 ``` @SpringBootConfiguration class MyStarter { @Autowired lateinit var myService: MyService } ``` 5. 创建一个META-INF文件夹,添加spring.factories文件,并在文件中添加自动配置类的引用。 ``` org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.MyAutoConfiguration ``` 6. 构建和安装Maven或Gradle包,可以使用以下命令: ``` ./gradlew build ./gradlew publishToMavenLocal ``` 7. 在其他Spring Boot应用程序中使用启动器,可以在build.gradle文件中添加以下依赖: ``` dependencies { implementation("com.example:my-starter:1.0.0") } ``` 8. 在应用程序中使用自动配置的Bean。 ``` @RestController class MyController(val myService: MyService) { @GetMapping("/hello") fun hello(): String { return myService.getMessage() } } ``` 这样,就可以通过Kotlin构建Spring Boot Starter,并在其他应用程序中使用自动配置的Bean了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值