简体   繁体   English

Spring Boot @Value 注解从 application.properties 中获取密钥,但在运行时不使用它

[英]Spring Boot @Value annotation picks up key from application.properties, but does not use it at run time

The value market with @Value isn't getting passed during run time. @Value 的价值市场在运行时没有通过。 My code looks like this:我的代码如下所示:

import com.example.MyClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    @Value("${api.key}")
    private String apiKey;

@Bean
public MyClass getKey() {

    return new MyClass(apiKey);

}

} }

When I hover my mouse over ${api.key} in IntelliJ it shows the key.当我 hover 我的鼠标悬停在 IntelliJ 中的 ${api.key} 上时,它会显示密钥。 And if I replace ${api.key} with the key itself, everything works perfectly fine.而且,如果我将 ${api.key} 替换为密钥本身,则一切正常。 So the IDE knows that ${api.key} is the key from application.properties, but on run time, it doesn't use it, and I get a 401 error (because the field is null).所以 IDE 知道 ${api.key} 是来自 application.properties 的键,但是在运行时,它不使用它,并且我收到 401 错误(因为该字段为空)。 Even if I go to application.properties and use "Find usages" on the api.key, it shows the right place in the code where it is used.即使我 go 到 application.properties 并在 api.key 上使用“查找用法”,它也会在代码中显示使用它的正确位置。

If I run mvn spring-boot:run, everything executes perfectly like it should, except the api-key is left null.如果我运行 mvn spring-boot:run,一切都会像它应该的那样完美执行,除了 api-key 是 null。 If I write the key as a string instead of using @Value, it reads it and everything works like it should.如果我将密钥写为字符串而不是使用@Value,它会读取它并且一切都按预期工作。 It seems to be centered around that @Value isn't passed, although Spring Boot works in every other regard.尽管 Spring Boot 在其他所有方面都有效,但似乎集中在 @Value 未通过。

I have exhausted all suggestions I've found online, and nothing works.我已经用尽了我在网上找到的所有建议,但没有任何效果。 It's hard to list all attempts I've made, but I've tried everything I've seen.很难列出我所做的所有尝试,但我已经尝试了我所看到的一切。 Configuring the pom, project structure, run configurations, adding @PropertySource, settings, and more.配置 pom、项目结构、运行配置、添加 @PropertySource、设置等。 I even created the whole project from scratch to see if something had gotten tangled up while I tried to solve it.我什至从头开始创建了整个项目,以查看在我尝试解决问题时是否有问题。

It recognizes the properties, it runs Spring, it recognizes the key, but it just doesn't use it.它识别属性,运行 Spring,识别密钥,但只是不使用它。

I have faced a similar issue.我遇到了类似的问题。 I find that you can use the @Value annotation on a setter.我发现您可以在 setter 上使用 @Value 注释。 This should work:这应该有效:

private String apiKey;
@Value("${api.key}")
    public String setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

I cannot not explain why or how this work, so if someone has any further explanations feel free to edit my answer.我不能解释为什么或如何工作,所以如果有人有任何进一步的解释,请随时编辑我的答案。

Try the following approach:尝试以下方法:


@Configuration
public class MyConfig {
    
   @Bean
   public MyClass getKey(@Value("${api.key}") String apiKey) {
       return new MyClass(apiKey);
   }
}

Another (even better) option is using configuration properties:另一个(甚至更好)的选择是使用配置属性:

@ConfigurationProperties(prefix="api")
public class ApiConfigProperties {
   private String key;
   // getters, setters, no-op constructor
}

@Configuration
@EnableConfigurationProperties(ApiConfigProperties.class)
public class MyConfig {
    
  @Bean 
  public MyClass getKey(ApiConfigProperties config) {
      return new MyClass(config.getKey()); 
  } 

}

I usually prefer the second approach because it allows (with a plugin) the generation of a "special" JSON file that the IDE can read and support autocompletion.我通常更喜欢第二种方法,因为它允许(使用插件)生成 IDE 可以读取并支持自动完成的“特殊” JSON 文件。 Another benefit is that the second way is easily debuggable (place a breakpoint in the setter - can't be easier) and allows the defaults specified in one place.另一个好处是第二种方法很容易调试(在设置器中放置一个断点 - 再简单不过了)并允许在一个地方指定默认值。

As for your option suggested in the question, I usually treat @Configuration as a DSL that creates beans, so I don't maintain any state on it.至于问题中建议的选项,我通常将@Configuration视为创建 bean 的 DSL,因此我不会在其上维护任何 state。 Since Configuration has happened to be a spring bean, it allows the auto wiring, but it doesn't mean that you should use it.由于 Configuration 恰好是一个 spring bean,它允许自动布线,但这并不意味着您应该使用它。

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

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