1. 配置依赖
在 Springboot 项目中使用 Kotlin, 需要在 pom|pom.xml 文件中配置 Kotlin 依赖。
<properties>
<!--kotlin版本-->
<kotlin.version>2.2.0</kotlin.version>
</properties>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
一般Java项目中会使用 Lombok, 需要添加 Lombok 依赖
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-lombok</artifactId>
<version>${kotlin.version}</version>
</dependency>
Springboot中使用了大量的反射,这时候必须添加 Kotlin 的反射依赖包,否则有可能在反射设置Bean的时候出现无法注入Bean的问题
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
Web项目中默认使用的 Jackson 序列化接口返回的数据,这时候需要添加 Kotlin 的 Jackson 依赖。
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
2. 配置构建插件
配置 Kotlin 的 Maven 构建插件,这时候需要注意几个问题。
- 需要添加 Lombok 插件,可解决 Java 与 Kotlin 混合编译时,Java 类访问 Kotlin 类的 Getter/Setter 方法无法解析的问题。
- 添加 Spring 的
noarg和allopen插件,这样我们在写 Controller, Service 这些类的时候不需要手动添加 open 类标识符和构造函数了,会在编译器自动将Bean类编译为open类型和添加无参构造函数。这么做的原因如下:- Spring 为Bean生成的代理需要继承目标类,但是 Kotlin 默认的 Class 类型是 final类型不可继承的,会导致无法生成代理。
- Kotlin 默认的 Class 未显示定义无参构造函数情况下是不生成构造函数的,这可能导致 Spring 在构造对象的时候找不到构造函数而无法生成对象。
<build>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<!-- 添加编译插件 -->
<compilerPlugins>
<!--lombok解决javalombok插件问题-->
<plugin>lombok</plugin>
<plugin>jpa</plugin>
<!--spring插件辅助生成open类与无参构造函数-->
<plugin>spring</plugin>
</compilerPlugins>
<args>
<!--开启严格的jsr305校验,添加后kotlin会严格校验Java代码中那些@Notnull之类的参数,比如标注非空的代码传入了空值,会在编译期间就编译失败-->
<arg>-Xjsr305=strict</arg>
</args>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>target/generated-test-sources/test-annotations</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<!--添加插件需要的依赖-->
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-lombok</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</build>
使用Kotlin编码
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}