gpt4 book ai didi

java - 无法在 spring-boot 中从属性文件和 Bootstrap.yml 中选取值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:41 29 4
gpt4 key购买 nike

我想从 application.propertiesbootstrap.yml 文件中选取值。当我尝试这样做时,我在控制台上得到 Null。请建议这里有什么问题。

application.properties如下

restcall.erp.name=erp
restcall.plant.name=plant
restcall.apikey.name=apikey
restcall.region.name=region

bootstrap.yml如下

apigee:
api:
key: someKey
url:
item:
itemputurl: https://someAnotherUrl

**Java代码**

  package com.jci.itemjob.service.mapics;

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


//@RefreshScope
@Component
//@Configuration
//@ComponentScan
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:bootstrap.yml"),
})
public class MapicsSendAckToApigeeFromItemJob {


@Value("${apigee.url.item.itemputurl}")
private String itemPUTURL;

@Value("${restcall.erp.name}")
private String erpParam;

@Value("${restcall.plant.name}")
private String plantParam;

@Value("${restcall.region.name}")
private String regionParam;

@Value("${restcall.apikey.name}")
private String apiKeyParam;

@Value("${restcall.params.key.headers}")
private String headersParam;

private static final Logger LOG = LoggerFactory.getLogger(MapicsSendAckToApigeeFromItemJob.class);

public CloseableHttpResponse sendResponse(String apikey, String plant, String erp, String region,
MapicsItemApigeePut itemApigeePut) {


try {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();

LOG.info("erp-----------------" + erpParam);
LOG.info("plant-----------------" + plantParam);
LOG.info("apikey-----------------" + apiKeyParam);
LOG.info("region-----------------" + regionParam);
LOG.info("itemPUTURL-----------------" + itemPUTURL);

//Other Logic

输出如下

item-job-mapics_1  | 2018-02-05 09:02:35.381  INFO 1 --- [nio-9011-exec-3] j.i.s.m.MapicsSendAckToApigeeFromItemJob : erp-----------------null
item-job-mapics_1 | 2018-02-05 09:02:35.381 INFO 1 --- [nio-9011-exec-3] j.i.s.m.MapicsSendAckToApigeeFromItemJob : plant-----------------null
item-job-mapics_1 | 2018-02-05 09:02:35.381 INFO 1 --- [nio-9011-exec-3] j.i.s.m.MapicsSendAckToApigeeFromItemJob : apikey-----------------null
item-job-mapics_1 | 2018-02-05 09:02:35.381 INFO 1 --- [nio-9011-exec-3] j.i.s.m.MapicsSendAckToApigeeFromItemJob : region-----------------null
item-job-mapics_1 | 2018-02-05 09:02:35.381 INFO 1 --- [nio-9011-exec-3] j.i.s.m.MapicsSendAckToApigeeFromItemJob : itemPUTURL-----------------null

最佳答案

我不是说你需要做所有这些,但我就是这么做的......我创建了带注释的 AppProperties 类

@Component
@ConfigurationProperties(prefix="app")
@PropertySources({
@PropertySource("classpath:application.properties")
// @PropertySource("classpath:application-local.properties"),
// @PropertySource("classpath:application-dev.properties"),
// @PropertySource("classpath:application-prod.properties"),
})
public class AppProperties {

@NotNull
private String name;

}

在属性文件中我有 app.name

ConfigurationProperties 定义您在配置中的前缀。 Spring 可以自动找到你的属性文件,但我发现,当我有配置文件“dev”时,它只读取 application-dev.properties。使用 @PropertySource("classpath:application.properties") 它会读取该文件 + 配置文件特定的文件(这是我一直在寻找的东西)。

另外我还有一个类(空的):

@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfiguration {
}

关于java - 无法在 spring-boot 中从属性文件和 Bootstrap.yml 中选取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48618481/

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