《Springboot极简教程》Springboot使用Kotlin和Java混合编程
来源:互联网 发布:web优化 sysctl 编辑:程序博客网 时间:2024/06/06 01:20
配置
要想实现Springboot使用Kotlin和Java混合编程,需要使用kotlin插件:
spring-boot-gradle-plugin
kotlin-gradle-plugin
具体配置如下:
apply { plugin "kotlin" plugin "kotlin-spring" plugin "kotlin-jpa" plugin "org.springframework.boot" plugin 'java' plugin 'eclipse' plugin 'idea' plugin 'war' plugin 'maven'}sourceSets { main { kotlin { srcDir "src/main/kotlin" } java { srcDir "src/main/java" } } test { kotlin { srcDir "src/test/kotlin" } java { srcDir "src/test/java" } }}buildscript { ext { kotlinVersion = '1.1.0' springBootVersion = '1.5.2.RELEASE' } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion" classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion" }}dependencies { compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4") ...}
完整的配置参考:
https://github.com/Jason-Chen-2017/restfeel/blob/master/build.gradle
工程目录结构
完整的工程目录,可以参考:
https://github.com/Jason-Chen-2017/restfeel
把工程git clone下来,命令行tree一下,即可看到。
让kotlin代码的归kotlin目录,java代码归java目录
比如说,我们把SpringBoot启动类放到kotlin下面:
package com.restfeelimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.CommandLineRunnerimport org.springframework.boot.SpringApplicationimport org.springframework.core.env.Environment/** * Created by jack on 2017/3/29. * @author jack * @date 2017/03/29 */@RestFeelBootclass RestFeelApplicationKotlin : CommandLineRunner { @Autowired private val env: Environment? = null override fun run(vararg args: String?) { println("RESTFEEL 启动完毕") println("应用地址:" + env?.getProperty("application.host-uri")) }}fun main(args: Array<String>) { SpringApplication.run(RestFeelApplicationKotlin::class.java, *args)}
执行
gradle bootRun
系统会正常运行的。
Java代码Kotlin代码互相调用
我们放到后面单独的一章来讲。
阅读全文
0 0
- 《Springboot极简教程》Springboot使用Kotlin和Java混合编程
- 《Spring Boot极简教程》第10章 Springboot集成Kotlin混合Java开发
- 《Springboot极简教程》 Springboot plus Kotlin :Hello,World
- 《Groovy极简教程》第5章 SpringBoot集成Groovy与Java混合编程
- Kotlin集成 SpringBoot 混合Java库开发
- 《Kotin 极简教程》第11章 使用Kotlin 集成 SpringBoot开发Web服务端
- Springboot极简教程
- Springboot极简教程
- 《Springboot极简教程》运行SpringBoot程序
- 《Springboot极简教程》SpringBoot配置文件PropertySourcesPlaceholderConfigurer
- 《Kotlin极简教程》第七章 Kotlin 集成 Springboot开发WebApp
- 《Springboot极简教程》使用@SpringBootApplication annotation
- 《Springboot极简教程》使用Spring Boot, JPA, Mysql, ThymeLeaf,gradle, Kotlin快速构建一个CRUD Web App
- 使用kotlin开发springboot应用
- springboot 使用教程
- 《Springboot极简教程》SpringBoot热部署应用Developer tools
- kotlin之springboot小试牛刀
- kotlin之springboot二
- 《Springboot极简教程》问题解决:Idea解决Gradle库依赖冲突问题:Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting
- 传递WinMain的参数给main
- 《Springboot极简教程》问题解决:org.apache.catalina.LifecycleException: A child container failed during start
- 邮件发送和接收原理
- 【tyvj1098】任务安排(dp)
- 《Springboot极简教程》Springboot使用Kotlin和Java混合编程
- Elam的caffe笔记之配置篇(六):Centos6.5下编译caffe及caffe的python3.6接口
- 《Springboot极简教程》@EnableMongoRepositories路径配置:APPLICATION FAILED TO START: Parameter 0 of constructor in
- 《Springboot极简教程》MappingMongoConverter:Failed to convert from type [java.lang.String] to type [long] for value 'null'; ne
- 《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler
- 《JSP极简教程》jsp c:forEach用法
- RequireJS极简入门教程
- 分布式系统理论进阶
- 比CPU效率高30倍阿里云推出FPGA人工智能加速服务