Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 带有Spring Boot 2.0@ConfigurationProperties的Kotlin不工作_Spring Boot_Intellij Idea_Kotlin - Fatal编程技术网

Spring boot 带有Spring Boot 2.0@ConfigurationProperties的Kotlin不工作

Spring boot 带有Spring Boot 2.0@ConfigurationProperties的Kotlin不工作,spring-boot,intellij-idea,kotlin,Spring Boot,Intellij Idea,Kotlin,我正在使用SpringBoot2和Kotlin构建一个应用程序 不知怎的,我就是无法让ConfigurationProperties工作 据我所知,当Mavencompile运行时,应该在target/classes/META-INF 到目前为止,我的设置: pom.xml LogDbApplication.kt import org.springframework.beans.factory.annotation.Autowired 导入org.springframework.boot.Com

我正在使用SpringBoot2和Kotlin构建一个应用程序

不知怎的,我就是无法让ConfigurationProperties工作

据我所知,当Maven
compile
运行时,应该在
target/classes/META-INF

到目前为止,我的设置:

pom.xml LogDbApplication.kt
import org.springframework.beans.factory.annotation.Autowired
导入org.springframework.boot.CommandLineRunner
导入org.springframework.boot.autoconfigure.springboot应用程序
导入org.springframework.boot.runApplication
@SpringBoot应用程序
类LogdbApplication:CommandLineRunner{
覆盖趣味跑步(vararg args:String?){
println(logDbProperties.enabled)
}
@自动连线
lateinit var logDbProperties:logDbProperties
}
趣味主线(args:Array){
运行应用程序(*args)
}
我怎样才能让它工作

更新 这些注释似乎是由Spring获取的,但IntelliJ只是没有创建
Spring配置元数据.json
文件,这意味着它只是自动完成不起作用

那么,如何让IntelliJ创建
spring配置元数据.json
文件呢?

请参阅关于配置属性生成的内容

很快,您必须将此添加到maven中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

org.springframework.boot
spring引导配置处理器
真的
重要提示:IntelliJ idea无法与kapt一起工作,后者将生成元数据。因此,您必须要求gradle/maven进行完整构建。因此:

  • 您的输出文件夹将生成
    spring配置元数据.json
  • 您的输出jar也将包含此文件
  • IntelliJ Idea将读取此文件并显示亮点 多亏了你,我终于有了答案

    要使解决方案完整,请执行以下操作:

    将以下依赖项添加到pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency> 
    
    现在是ConfigurationProperties类的一个示例:

    @ConfigurationProperties(prefix = "logdb")
    class LogDbProperties {
        var enabled: Boolean = false
    }
    
    现在运行
    mvn compile
    mvn clean compile


    普雷斯托:在
    target/classes/META-INF
    中有一个名为
    spring configuration metadata.json
    的文件,我面临着同样的问题,尽管我遵循了这个建议,但没有任何变化。IntelliJ 2018.1.3,Kotlin 1.2.41,SpringBoot 2.0.1这不是答案。OP pom.xml确实具有此依赖关系,元数据生成在此设置中不起作用在此解决方案中,enabled的默认值为false,如果您不想提供默认值,也不想允许null(合理的请求),则可以执行:
    @ConfigurationProperties(prefix=“logdb”)类LogDbProperties{lateinit var enabled:Boolean=false}
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency> 
    
    <execution>
        <id>kapt</id>
        <goals>
            <goal>kapt</goal>
        </goals>
        <configuration>
            <sourceDirs>
                <sourceDir>src/main/kotlin</sourceDir>
            </sourceDirs>
            <annotationProcessorPaths>
                <annotationProcessorPath>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-configuration-processor</artifactId>
                    <version>1.5.3.RELEASE</version>
                </annotationProcessorPath>
            </annotationProcessorPaths>
        </configuration>
    </execution>
    
    @ConfigurationProperties(prefix = "logdb")
    class LogDbProperties {
        var enabled: Boolean = false
    }