[英]Can Kotlin controller class access Java service class in spring boot
I a working on a project in spring boot.. is it possible to use in kotlin and java both in spring-boot project. I a working on a project in spring boot.. is it possible to use in kotlin and java both in spring-boot project. I have tried by making a Kotlin @controller class and calling java @service class but it isn't working
我尝试制作 Kotlin @controller class并调用 java @service ZA2F2ED4F8EBC2CBB4C21A29 ,但它不是工作
Yes, it will definitely work是的,它肯定会起作用
You simply need to configure it properly您只需要正确配置它
check your build.gradle source sets and targets检查您的 build.gradle 源集和目标
Follow the below folder structure遵循以下文件夹结构
src
main
java
kotlin
resources
I have done the following to successfully use Kotlin in my existing Spring Boot App.我已完成以下操作以在我现有的 Spring 启动应用程序中成功使用 Kotlin。 My IDE is IntelliJ.
我的 IDE 是 IntelliJ。
(1) (1)
Ensure that Kotlin plugin is installed in the IDE.确保 Kotlin 插件安装在 IDE 中。
(2) (2)
Get the required dependencies.获取所需的依赖项。
…
<properties>
…
<kotlin.version>1.6.10</kotlin.version>
<!--Edit the version to the latest stable version, if applicable-->
</properties>
…
<dependencies>
…
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
</dependencies>
…
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
…
</build>
…
<plugins>
…
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
(3) (3)
Create the required directories in your project.在您的项目中创建所需的目录。
(4) (4)
Now update / rebuild the project.现在更新/重建项目。 If any Maven / build errors show up then do one / all of the following:
如果出现任何 Maven / 构建错误,请执行以下一项/所有操作:
(5) (5)
Now you can create a Kotlin class under the src/main/kotlin package.现在您可以在 src/main/kotlin package 下创建 Kotlin class。 For this,
为了这,
Note:笔记:
If the Kotlin class does not have a package statement then it will not be visible from your Java classes.如果 Kotlin class 没有 package 语句,则它不会从您的 ZD52387880E1EA213817A 类中看到。
To fix this,为了解决这个问题,
Go to your Java class which has the main method. Go 到您的 Java class 具有主要方法。 (This is the class which is annotated by @SpringBootApplication)
(这是由@SpringBootApplication 注释的class)
Copy the package statement of this class.复制此 class 的 package 语句。 This should most likely be the first statement of the Java class and should be something like
这很可能是 Java class 的第一条语句,应该类似于
package com.xyz.myApplication;
Paste this statement in the Kotlin class so that this becomes the first statement of your Kotlin class as well.将此语句粘贴到 Kotlin class 中,使其成为 Kotlin ZA2F2ED4F8EBC0CBB4C21A29D 的第一条语句。
Once everything is done, you can simply import and instantiate your Kotlin classes from inside your Java classes.一切完成后,您可以从 Java 类中简单地导入和实例化 Kotlin 类。 You can also do vice versa, that is, import and instantiate your Java classes from inside your Kotlin classes.
您也可以反之亦然,即从 Kotlin 类中导入并实例化您的 Java 类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.