我想从 application.properties 和 bootstrap.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 Code **
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
答案 0 :(得分:0)
我并不是说你需要做所有这些,但这就是我所做的......我创建了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可以自动找到你的属性文件,但我发现,当我有个人资料&#34; dev&#34;它只是在阅读application-dev.properties。使用@PropertySource("classpath:application.properties")
,它会读取该文件+配置文件特定的一个(这是我正在寻找的东西)。
此外,我还有一个课程(空白):
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfiguration {
}