gpt4 book ai didi

java - 从 yaml 文件打印属性时为 Null

转载 作者:行者123 更新时间:2023-12-02 09:40:35 46 4
gpt4 key购买 nike

我正在尝试通过执行以下操作来访问 yml 文件中的属性:

import org.springframework.beans.factory.annotation.Value;

@SpringBootApplication
public class Main {

@Value("${external.library.url.empik.best.accuracy.book}")
private static String bestAccuracyBook;

public static void main(String[] args) throws IOException {
SpringApplication.run(Main.class, args);
System.out.println(bestAccuracyBook);
}

}

尽管添加了 SprnigBoot 符号,但它仍打印 null。不知道为什么。

我的 application-local.yml 文件如下所示:

external:
library:
url:
meriln:
romans: www.melin.pl/romans
sensacja: www.melin.pl/sensacja
empik:
guides: https ://www.empik.com/ksiazki/poradniki
romances: https://www.empik.com/ksiazki/romans
biographies: https://www.empik.com/ksiazki/biografie-i-dokument
bestsellers: https://www.empik.com/bestsellery/ksiazki
crime: https://www.empik.com/ksiazki-sensacja-kryminal
fantasy: https://www.empik.com/ksiazki/literatura-fantastyka
concrete.book: https://www.empik.com/%s,%s,ksiazka-p
best.accuracy.book: https://www.empik.com/ksiazki,31,s?sort=scoreDesc&q=%s

我注意到,当我用鼠标点击任何属性时,它会显示:无法解析配置属性.....

我的pom.xml

....

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>

...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

最佳答案

您不能将 @Value 放在静态字段上。

关于java - 从 yaml 文件打印属性时为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101676/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com