将Spring Boot应用程序的`application.properties`中的环境变量获取到配置类中。

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

get springboot application.properties env variables into config class

问题

以下是您要翻译的内容:

I am struggling to create a config class that holds final strings of configuration properties that I use elsewhere. I don't want to use the Environment. So I have some code

@Autowire
Environment env;

return this webClient.get()
.uri(uriBuilder -> uriBuilder.path(env.getProperty("endpoint")).build())
.accept(MediaType.TEXT_EVENT_STREAM)
.exchangeToFlux(e -> e.bodyToFlux(Event.class));

That's actually working fine, but I want to get my config together in a separate class, out of the environment. Like:

@Configuration
@PropertySource("classpath:application.properties")
public class MyConfig {

@Value("${endpoint}")
public static final String URI = ""

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}

In my springboot project when I change from env to use the MyConfig variable:

return this webClient.get()
.uri(uriBuilder -> uriBuilder.path(MyConfig.URI).build())
.accept(MediaType.TEXT_EVENT_STREAM)
.exchangeToFlux(e -> e.bodyToFlux(Event.class));

When I get a unit test going that's going to run the call to the URI config from the MsgConfig class it's always null. How can I get the thing to populate?

UPDATE

It looked as if removing the static from

public static final String URI = "";

and @Autowire the MyConfig class into the other class was going to work, and indeed sometimes it does, but this appears to cause a race condition whether the URI is set or not...

英文:

I am struggling to create a config class that holds final strings of configuration properties that I use elsewhere. I don't want to use the Environment. So I have some code

@Autowire
Environment env;

return this.webClient.get()
  .uri(uriBuilder -> uriBuilder.path(env.getProperty("endpoint")).build())
  .accept(MediaType.TEXT_EVENT_STREAM)
  .exchangeToFlux(e -> e.bodyToFlux(Event.class));

That's actually working fine, but I want to get my config together in a separate class, out of the environment. Like:

@Configuration
@PropertySource("classpath:application.properties")
public class MyConfig {

  @Value("${endpoint}")
  public static final String URI = "";

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
  	return new PropertySourcesPlaceholderConfigurer();
  }
}

In my springboot project when I change from env to use the MyConfig variable:

return this.webClient.get()
  .uri(uriBuilder -> uriBuilder.path(MyConfig.URI).build())
  .accept(MediaType.TEXT_EVENT_STREAM)
  .exchangeToFlux(e -> e.bodyToFlux(Event.class));

When I get a unit test going that's going to run the call to the URI config from the MsgConfig class it's always null. How can I get the thing to populate?

UPDATE

It looked as if removing the static from

public static final String URI = "";

and @Autowire the MyConfig class into the other class was going to work, and indeed sometimes it does, but this appears to cause a race condition whether the URI is set or not...

答案1

得分: 1

我建议使用一个配置属性类,如Spring Boot文档中所述。首先,创建一个类来保存配置属性:

@ConfigurationProperties("endpoint")
public class EndpointProperties {

    private final URI uri;

    public EndpointProperties(URI uri) {
        this.uri = uri;
    }

    public URI getUri() {
        return this.uri;
    }
}

然后,使用文档中描述的其中一种技术来启用EndpointProperties

然后,将它注入到需要这些属性的类中:

@Autowired
EndpointProperties endpoint;

return this.webClient.get()
  .uri(uriBuilder -> uriBuilder.path(endpoint.getUri()).build())
  .accept(MediaType.TEXT_EVENT_STREAM)
  .exchangeToFlux(e -> e.bodyToFlux(Event.class));

在Spring Boot的application.yml文件中,你可以设置属性值如下:

endpoint:
  uri: https://example.com/api/
英文:

I would suggest using a configuration properties class, as described in the Spring Boot documentation.

First, create a class to hold the configuration properties:

@ConfigurationProperties("endpoint")
public class EndpointProperties {

    private final URI uri;

    public MyProperties(URI uri) {
        this.uri = uri;
    }

    public URI getUri() {
        return this.uri;
    }
}

Then enable the EndpointProperties using one of the techniques described in the documentation.

You can then inject this into the class that needs the properties:

@Autowired EndpointProperties endpoint;

return this.webClient.get()
  .uri(uriBuilder -> uriBuilder.path(endpoint.getUri()).build())
  .accept(MediaType.TEXT_EVENT_STREAM)
  .exchangeToFlux(e -> e.bodyToFlux(Event.class));

In a Spring Boot application.yml file, you'd set the property value like this:

endpoint: 
  uri: https://example.com/api/

huangapple
  • 本文由 发表于 2023年2月7日 00:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364226.html
匿名

发表评论

匿名网友

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

确定