无法在函数中获取定义的变量值。

huangapple go评论75阅读模式
英文:

Not able to get defined variable value in function

问题

我有以下的代码。

public class TestClass {

  @Value("${api.key}")
  private String apiKey;

  @Value("${api.version}")
  private String apiVersion;

  private String baseURL = apiKey + "/" + apiVersion;

  public test() {
     LOGGER.debug("Val {}", baseURL);
  }
}

当我尝试运行它时,始终返回 null 作为结果。

我有相同的 baseURL,并且在一个类中有多个函数,所以如果有任何更改,不需要在每个函数中进行更改。

英文:

I have below code.

public class TestClass {

  @Value("${api.key}")
  private String apiKey;

  @Value("${api.version}")
  private String apiVersion;

  private String baseURL = apiKey + "/" + apiVersion;

  public test() {
     LOGGER.debug("Val {}", baseURL);
  }
}  

When I try to run it always give null as a result.

I have same baseURL and in Once class I have multiple functions, so if any changes come no need to change in every function.

答案1

得分: 3

Static values are initialised before the beans creation, which means the values to the member fields will be injected after the static variable is assigned with values and thats the reason it's coming null.

What can be done is, you can use Spring's @PostConstruct above an init function and perform your operation there and assign the base url to the member variable, which can be accessed later.
Post Construct makes sure your code inside function is executed(only once) after your bean gets created.

public class TestClass {

  @Value("${api.key}")
  private String apiKey;

  @Value("${api.version}")
  private String apiVersion;

  private final String baseURL;

  @PostConstruct
  private init() {
    baseURL = apiKey + "/" + apiVersion;
  }

  public String getBaseUrl() {
    return baseUrl;
  }

  public test() {
     LOGGER.debug("Val {}", baseURL);
  }
}
英文:

Static values are initialised before the beans creation, which means the values to the member fields will be injected after the static variable is assigned with values and thats the reason it's coming null.

What can be done is, you can use Spring's @PostConstruct above an init function and perform your operation there and assign the base url to the member variable, which can be accessed later.
Post Construct makes sure your code inside function is executed(only once) after your bean gets created.

    
public class TestClass {

  @Value("${api.key}")
  private String apiKey;

  @Value("${api.version}")
  private String apiVersion;

  private final String baseURL;

  @PostConstruct
  private init() {
    baseURL = apiKey + "/" + apiVersion;
  }

  public String getBaseUrl() {
    return baseUrl;
  }

  public test() {
     LOGGER.debug("Val {}", baseURL);
  }
}  

答案2

得分: 0

This is because static members will be initialized before the value of configKey will be injected. You can access configKey only if your TestClass is fully initialized. You can try something like this:

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Log4j
@Component
public class TestClass {

    @Value("${config.key}")
    private String configKey;

    @PostConstruct
    void initialized() {
        this.test();
    }

    public void test() {
        log.debug(String.format("configKey: '%s'", this.configKey));
    }
}

And you should insert the following line into your application.properties:

config.key=something
英文:

This is because static members will be initialized before the value of configKey will be injected. You can access configKey only if your TestClass is fully initialized. You can try something like this:

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Log4j
@Component
public class TestClass {

    @Value("${config.key}")
    private String configKey;

    @PostConstruct
    void initialized() {
        this.test();
    }

    public void test() {
        log.debug(String.format("configKey: '%s'", this.configKey));
    }
}

And you should insert the following line into your application.properties:

config.key=something

huangapple
  • 本文由 发表于 2020年8月11日 16:26:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63354342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定