簡體   English   中英

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

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

我正在使用Spring Boot 2和Kotlin構建一個應用程序。

不知何故,我無法讓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文件呢?

感謝StackOverflow上的這篇文章,我終於得到了答案

只是為了完成解決方案:

將以下依賴項添加到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 compilemvn clean compile

並且presto:你在target/classes/META-INF有一個名為spring-configuration-metadata.json的文件。

請參閱有關配置屬性生成的官方Spring文檔

不久,你必須將它添加到maven中:

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

重要提示:IntelliJ想法無法與kapt一起使用,后者將生成元數據。 因此,您必須要求gradle / maven進行完整構建。 結果是:

  1. 您的輸出文件夾將生成spring-configuration-metadata.json
  2. 你的輸出jar也會有這個文件
  3. IntelliJ Idea將讀取此文件並顯示高亮顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM