Spring boot 带有Spring Boot 2.0@ConfigurationProperties的Kotlin不工作
我正在使用SpringBoot2和Kotlin构建一个应用程序 不知怎的,我就是无法让ConfigurationProperties工作 据我所知,当MavenSpring 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
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
<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
}