【问题标题】:Spring @Value returns <empty>Spring @Value 返回 <empty>
【发布时间】:2021-12-31 22:23:46
【问题描述】:

我检查了类似的问题:

Spring - @Value returns null

Spring Boot: @Value returns always null

但是,我找不到我的注释有什么问题

这是代码在 IntelliJ 中的外观 screenshot from IntelliJ

下面是这个

@Component
public class VisitMapper {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

//more code below

我有两个 .properties 文件:application-dev.properties 和 application.properties。 活动配置文件设置为 dev。 在 application-dev.properties 我有:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

不确定它是否重要,因为活动配置文件设置为 dev,但在 application.properties 我有:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

总结一下-为什么密码值显示为空?

【问题讨论】:

  • 你真的运行过代码吗?我怀疑它不是空的,并且您被 IDE 中的某些东西抛弃了(这可能会隐藏密码以防窥探)。
  • 我在运行代码时遇到了一些问题,可能与此问题无关。应用程序启动,但是当我尝试调试和发送任何请求时,我得到了错误。
  • 如果它启动它就被解决了,如前所述,您可能会被 IDE 抛出(除非您的错误与数据库相关,但这会阻止您的应用程序首先启动)。跨度>
  • 感谢您的回答。但是像“隐藏”这样的消息不会比“空”来阻止窥视者更好吗?
  • 您必须与 Jetbrains 的人员一起讨论这个问题。如果您的应用程序启动配置被正确读取,否则它会爆炸。

标签: spring postgresql spring-boot annotations


【解决方案1】:

我怀疑 Intellij 隐藏了带有 password 的字段(尽管 &lt;empty&gt; 有点糟糕),或者 Intellij 很难为该字段解析正确的属性值(无论出于何种原因)。

但是,您的应用程序启动的事实表明,至少,Spring Boot 确实正确解析了这些属性,否则它不会启动并崩溃。由于无法解析属性或无法连接到数据库,它不会启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多