简体   繁体   中英

Unable to pick the values from Properties file and Bootstrap.yml in spring-boot

I wanted to pick the values from the application.properties and bootstrap.yml file. When I try to do so I am getting Null on the console. Please suggest whats wrong here.

application.properties is as follows

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

bootstrap.yml is as follows

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

The Output is as below

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

I'm not saying you need to do all of that, but that's what I did... I created AppProperties class annotated

@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;

}

and in property file I have app.name .

ConfigurationProperties defines your prefix in configuration. Spring can find you property files automatically, but I found, when I have profile "dev" it is reading application-dev.properties only. With @PropertySource("classpath:application.properties") it reads that file + profile specific one (which is something I was looking for).

Additionally I have one more class (which is empty):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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