spring-bootazul-zulu

Not able to Read place holder properties from external file into application properties(spring-boot:2.7.4, JDK: ZULU11)


Not able to get placeholder property key, while reading place holders in the application-dev.yml from external file.

@SpringBootApplication
@EnableAutoConfiguration(exclude = {WebMvcAutoConfiguration.class })
@ComponentScan("com.test.example")
@PropertySources({ 
@PropertySource(value = "file:/Users/myuserid/external-service.properties")
})
public class MysampleApp {
    public static void main(String[] args) {
        SpringApplication.run(MysampleApp.class, args);
    }
}

application.yml

MySampleApp --- src -----main -- resources --config --application.yml --application-dev.yml

properties defined in application.yml

my-sample-app:
  props:
      url : ${external_url}

external-service.properties

external_url = http://domain/external-app/dates

ExternalServicePropConfig.java

@Component
@ConfigurationProperties(prefix = "my-sample-app.props")
@Data
public class ExternalServicePropConfig
{
   private String url;
}

JDK - Zulu11 SpringBoot - 2.7.4


Solution

  • I have struggled almost long hours and finally made it. The issue is with the property file which was having.

    Key Note: all property files will be injected into environment variable as a propertysource object.

    I have debugged little bit from this code from the Environment variable and saw some jibbarish key and values for my external resources property file. It seems when any property file is edited, then it is getting defaultly to RTF format. And renaming and saving this file with .properties file. This action is not saving the file actually into .properties file. This is the culprit with Mac. Got an another working property file and verified that the code is perfectly working fine.

    @SuppressWarnings("rawtypes")
        private Map<String, ?> displayEnvironmentVariables() {
            Map<String, Object> props = new HashMap<>();
    
            log.info("externalUrl: {}",environment.getProperty("external_url"));
    
            for (Iterator it = ((AbstractEnvironment) environment).getPropertySources() .iterator(); it.hasNext();){
                PropertySource propertySource = (PropertySource) it.next();
                log.info("property source name: {}", propertySource.getName());
                if(propertySource instanceof ResourcePropertySource) {
                    ((MapPropertySource) propertySource).getSource().forEach( (k, v) -> {
                        log.info("key:{}", k);
                        log.info("Value:{}", v);
                    });
                }
            }
            return props;
        }