简体   繁体   English

带有Spring Boot 2.0的Kotlin @ConfigurationProperties不能正常工作

[英]Kotlin with Spring Boot 2.0 @ConfigurationProperties not working

I'm building an application using Spring Boot 2 with Kotlin. 我正在使用Spring Boot 2和Kotlin构建一个应用程序。

Somehow I just can't get ConfigurationProperties to work. 不知何故,我无法让ConfigurationProperties工作。

As far as I know when Maven compile is run then a file spring-configuration-metadata.json should be created in target/classes/META-INF 据我所知,当运行Maven compile时,应该在target/classes/META-INF创建一个文件spring-configuration-metadata.json

My setup so far: 到目前为止我的设置:

pom.xml 的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 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 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)
}

How can I get this to work? 我怎样才能让它发挥作用?

Update 更新

It seems that the annotations are picked up by Spring, but IntelliJ just doesn't create the spring-configuration-metadata.json file, which means it's just autocompletions that's not working. 似乎Spring注释了注释,但是IntelliJ只是没有创建spring-configuration-metadata.json文件,这意味着它只是自动填充功能无法正常工作。

So how could I make IntelliJ create the spring-configuration-metadata.json file? 那么我怎么能让IntelliJ创建spring-configuration-metadata.json文件呢?

Thanks to this post on StackOverflow I finally have the answer 感谢StackOverflow上的这篇文章,我终于得到了答案

Just to make the solution complete: 只是为了完成解决方案:

Add the following dependency to pom.xml 将以下依赖项添加到pom.xml

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

And (this is what all other answers are missing) add this execution to the kotlin-maven-plugin 并且(这是所有其他答案都缺失的)将此执行添加到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>

Now an example ConfigurationProperties class: 现在是一个示例ConfigurationProperties类:

@ConfigurationProperties(prefix = "logdb")
class LogDbProperties {
    var enabled: Boolean = false
}

Now run mvn compile or mvn clean compile 现在运行mvn compilemvn clean compile

And presto: you have a file named spring-configuration-metadata.json in target/classes/META-INF . 并且presto:你在target/classes/META-INF有一个名为spring-configuration-metadata.json的文件。

Please see official Spring documentation regarding to configuration properties generation. 请参阅有关配置属性生成的官方Spring文档

Shortly, you have to add this into maven: 不久,你必须将它添加到maven中:

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

Important: IntelliJ idea could not work with kapt, which will generate metadata. 重要提示:IntelliJ想法无法与kapt一起使用,后者将生成元数据。 So you have to ask gradle/maven to make a full build. 因此,您必须要求gradle / maven进行完整构建。 As a result: 结果是:

  1. Your output folders will have spring-configuration-metadata.json generated. 您的输出文件夹将生成spring-configuration-metadata.json
  2. Your output jar will have this file too 你的输出jar也会有这个文件
  3. IntelliJ Idea will read this file and will show highlights. IntelliJ Idea将读取此文件并显示高亮显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM