带有 Spring Boot 2.0 @ConfigurationProperties 的 Kotlin 无法正常工作
Posted
技术标签:
【中文标题】带有 Spring Boot 2.0 @ConfigurationProperties 的 Kotlin 无法正常工作【英文标题】:Kotlin with Spring Boot 2.0 @ConfigurationProperties not working 【发布时间】:2018-08-25 07:02:17 【问题描述】:我正在使用带有 Kotlin 的 Spring Boot 2 构建一个应用程序。
不知何故,我无法让 ConfigurationProperties 工作。
据我所知,当 Maven compile
运行时,应该在 target/classes/META-INF
中创建一个文件 spring-configuration-metadata.json
到目前为止我的设置:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brabantia.log</groupId>
<artifactId>logdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>logdb</name>
<description>Logging database Brabantia</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.21</kotlin.version>
<spring.boot.version>2.0.0.RELEASE</spring.boot.version>
</properties>
<dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<!-- START SPRING BOOT DEPENDENCIES -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>$spring.boot.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>$spring.boot.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>$spring.boot.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>$spring.boot.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>$spring.boot.version</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>$spring.boot.version</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>$spring.boot.version</version>
<scope>runtime</scope>
</dependency>
<!-- END SPRING BOOT DEPENDENCIES -->
<!-- START FLYWAY DEPENDENCIES -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<!-- END FLYWAY DEPENDENCIES -->
<!-- START KOTLIN DEPENDENCIES -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>1.2.30</version>
</dependency>
<!-- END KOTLIN DEPENDENCIES -->
<!-- START DATABASE DEPENDENCIES -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- END DATABASE DEPENDENCIES -->
</dependencies>
<build>
<sourceDirectory>$project.basedir/src/main/kotlin</sourceDirectory>
<testSourceDirectory>$project.basedir/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.0.7</version>
<configuration>
<user>esb_logging_user</user>
<password>esb_logging_user</password>
<url>jdbc:sqlserver://localhost;database=esb_logging</url>
</configuration>
</plugin>
</plugins>
</build>
</project>
LogDbProperties.kt
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "logdb")
class LogDbProperties
var enabled: Boolean = false
LogDbApplication.kt
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class LogdbApplication: CommandLineRunner
override fun run(vararg args: String?)
println(logDbProperties.enabled)
@Autowired
lateinit var logDbProperties: LogDbProperties
fun main(args: Array<String>)
runApplication<LogdbApplication>(*args)
我怎样才能让它工作?
更新
似乎注释被 Spring 提取了,但 IntelliJ 只是没有创建 spring-configuration-metadata.json
文件,这意味着它只是自动完成功能不起作用。
那么我怎样才能让 IntelliJ 创建 spring-configuration-metadata.json
文件呢?
【问题讨论】:
我面临同样的问题,尽管遵循了建议,但没有任何变化。 IntelliJ 2018.1.3、Kotlin 1.2.41、SpringBoot 2.0.1 【参考方案1】:感谢this post on ***我终于有了答案
只是为了使解决方案完整:
在 pom.xml 中添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
并且(这是所有其他答案都缺少的)将此执行添加到kotlin-maven-plugin
<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 类:
@ConfigurationProperties(prefix = "logdb")
class LogDbProperties
var enabled: Boolean = false
现在运行 mvn compile
或 mvn clean compile
然后:您在target/classes/META-INF
中有一个名为spring-configuration-metadata.json
的文件。
【讨论】:
在此解决方案中,enabled 的默认值为 false,如果您不想提供默认值并且也不想允许 null(合理的请求),您可以执行以下操作:@ConfigurationProperties(prefix = "logdb") class LogDbProperties lateinit var enabled: Boolean = false
【参考方案2】:
有关配置属性生成,请参阅official Spring documentation。
很快,您必须将其添加到 maven 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
重要提示:IntelliJ idea 无法与 kapt 一起使用,这将生成元数据。因此,您必须要求 gradle/maven 进行完整构建。结果:
-
您的输出文件夹将生成
spring-configuration-metadata.json
。
你的输出 jar 也会有这个文件
IntelliJ Idea 将读取此文件并显示亮点。
【讨论】:
这不是答案。 OP pom.xml 确实具有这种依赖关系,并且元数据生成在此设置中不起作用以上是关于带有 Spring Boot 2.0 @ConfigurationProperties 的 Kotlin 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
How to configure spring boot through annotations in order to have something similar to <jsp-confi
迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用
Spring Boot使用thymeleaf模板时报异常:template might not exist or might not be accessible by any of the confi