【问题标题】:How to make main java method simpler in kotlin?如何在 kotlin 中使主要的 java 方法更简单?
【发布时间】:2018-02-16 16:19:19
【问题描述】:

我正在使用 gradle 应用程序插件:

apply plugin:'application'
mainClassName = "com.example.MyApplication"

我有以下主应用程序的 kotlin 代码:

@SpringBootApplication
class MyApplication {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            runApplication<MyApplication>(*args)
        }
    }
}

有什么办法可以简化这段代码以便不使用companion object@JvmStatic注解?

【问题讨论】:

    标签: java spring gradle kotlin


    【解决方案1】:

    查看 JetBrains/kotlin-examples 存储库中的 Gradle hello-world example

    总而言之,您可以这样做:

    // src/main/kotlin/demo/helloWorld.kt
    fun main(args: Array<String>) {
        println(getGreeting())
    }
    

    然后在您的build.gradle 文件中:

    apply plugin: 'kotlin'
    apply plugin: 'application'
    
    mainClassName = 'demo.HelloWorldKt'
    

    在您的特定情况下,如果您的文件名是 MyApplication.ktmainClassName 将是 com.example.MyApplicationKt。至于那个 Kotlin 文件,它可能包含以下代码:

    @SpringBootApplication
    class MyApplication 
    
    fun main(args: Array<String>) {
        runApplication<MyApplication>(*args)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 2018-02-20
      相关资源
      最近更新 更多